Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1640
Binding.DoNothing overrides current theme
posted

Hi,

I'm setting the forecolor of of a field based on another field of the record using a IValueConverter only under the condition that the ShowColor field of the record is a string that can represents a Color. It goes like this:

<igDP:CellBinding Property="Foreground" Target="CellValuePresenter"
Binding="{Binding Path=DataItem.ShowColor, Converter={StaticResource colorToForeBrushConverter}}"/>

And the ColorToForeBrushConverter goes like this:

public class ColorToForeBrushConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value is Color)
        {
            return new SolidColorBrush((Color)value);
        }
        else if (value is string)
        {
            string ColorString = (string)value;
            if (ColorString == "") //no specified Foreground
            {
                return Binding.DoNothing
            }
            else
            {
                return new SolidColorBrush((Color)(ColorConverter.ConvertFromString(ColorString)));
            }
        }
        return Binding.DoNothing
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {

       var color = new Color();
       if (value is SolidColorBrush)
       {
              color = (value as SolidColorBrush).Color;
       }
       return color;

    }
}

The problem is:

1. Does igDP:CellBinding support conditions?  Meaning to use the ColorToForeBrushConverter only If the DataItem.ShowColor !="".

2. If problem 1 cannot be achieved, ColorToForeBrushConverter returns Binding.DoNothing when DataItem.ShowColor is"" to avoid binding. And this overrides all theme based colors, the Foreground is always Black then. But I don't want it to be black, I want it still be theme specified color, which could vary with themes. How can I achieve this?

Thank you.


Parents
  • 34510
    Offline posted

    Hi anakincn,

    Your best option is most likely a DataTrigger.  The issue with the current approach is that the binding is completely replacing whatever value Foreground had beforehand.  It is 100% relying on your converter to provide it the value.  Returning Binding.DoNothing is just going to set the Foreground to black.

    If you use a DataTrigger, you can check to see if the value of ShowColor is something valid.  If it's valid you can then set a binding on the Foreground.  If ShowColor ends up becoming invalid, the DataTrigger will revert Foreground to it's previous value (the Theme color).  This approach requires a style targeting CellValuePresenter and this style must be BasedOn the CellValuePresenter style from the current theme.  I have attached an example project demonstrating this.

    DataGridForegroundBinding.zip
Reply Children