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.
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.
One more thing, Rob.
If the Theme of xamDataGrid is chaging during runtime, the BasedOn property of Style in your sample fails like this with inproper border coming from the based theme. Since BasedOn is not allowed to be set binding, is it possible for the Style to interfere only the text part rather than the overall CellValuePresenter including the border? I checked CellValuePresenter, and could not find a TextBlock or TextElement member in it.
Exactly!
Thank you, Rob.