Click here to Skip to main content
15,904,817 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How shall i bind the label Forground Color in my Xaml file to a Color property in View Model??


Thanks in advance
Posted

The normal way to do this is to use a value converter. Here's an example where I highlight negative numbers in red (typical in balance sheet type applications):
C#
namespace MyNamespace
{
  using System;
  using System.Globalization;
  using System.Windows.Data;
  using System.Windows.Media;

  /// <summary>
  /// Converter to determine whether or not a number is negative, and apply
  /// conditional formatting if it is.
  /// </summary>
  public class NegativeNumberColorConverter : IValueConverter
  {
    #region IValueConverter Members
    /// <summary>
    /// Convert from the value to the colour brush.
    /// </summary>
    /// <param name="value">The value to convert.</param>
    /// <param name="targetType">The parameter is not used.</param>
    /// <param name="parameter">The parameter is not used.</param>
    /// <param name="culture">The parameter is not used.</param>
    /// <returns>The populated colour brush.</returns>
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
      var brush = new SolidColorBrush(Colors.Black);

      if (value == null)
      {
        return brush;
      }

      decimal result;
      var test = decimal.TryParse(value.ToString(), out result);

      if (!test || result >= 0)
      {
        return brush;
      }

      return new SolidColorBrush(Colors.Red);
    }

    /// <summary>
    /// Unused in this implementation.
    /// </summary>
    /// <param name="value">The parameter is not used.</param>
    /// <param name="targetType">The parameter is not used.</param>
    /// <param name="parameter">The parameter is not used.</param>
    /// <param name="culture">The parameter is not used.</param>
    /// <returns>The parameter is not used.</returns>
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
      throw new NotImplementedException();
    }

    #endregion
  }
}
 
Share this answer
 
Comments
arun_pk 22-Jun-11 9:34am    
thanks this is working
Pete O'Hanlon 22-Jun-11 9:41am    
You're welcome my friend. Glad to be able to help.
Brian Hubbard 19-Sep-11 12:38pm    
Pete, Great article, I am having trouble binding to a DataGridCell, the ServiceOrderColor = -1, any thoughts? DATAGRIDTEXTCOLUMN header="Issue Date" binding="{Binding IssueDate, Converter={StaticResource intToDateTimeConverter},StringFormat={}{0:d}}" foreground="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=DataGrid}, Path=ServiceOrderColor, Converter={StaticResource negativeNumberColorConverter}}">

Thanks Brian
Pete O'Hanlon 19-Sep-11 13:23pm    
What happens if you set a breakpoint in the converter? My suspicion is that you have a problem with the relative source - but without seeing the VM and View, I couldn't really say for sure.
Foreground is not of type Color, its of type Brush.

If you create a property of type brush in your viewmodel and bind it works. However better approach to use a value converter to give you the appropriate brush based on property value in your viewmodel.
 
Share this answer
 
Comments
arun_pk 22-Jun-11 9:33am    
Brush worked
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900