Question

I have my Multivalue Converter :

class ColorMultiConverter:IMultiValueConverter
    {

        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (values != null)
            {
                if (values[0] != DependencyProperty.UnsetValue && values[1] != DependencyProperty.UnsetValue)
                {
                    var customerRating = Int32.Parse(values[0].ToString());
                    var customerName = values[1].ToString();
                    if (customerName == "RaOne" && customerRating > 7)
                    {
                        return "Blue";
                    }
                }
                else
                    return "Yellow";

            }
            return "Red";
        }

In XAML , I am binding them as:

<DataGrid.RowStyle>
                <Style TargetType="DataGridRow">
                    <Style.Setters>
                        <!--<Setter Property="Background" Value="{Binding CustomerRating,Converter={StaticResource colorConverter}}">-->
                        <Setter Property="Background">
                            <Setter.Value>
                                <MultiBinding Converter="{StaticResource colorMultiConverter}">
                                    <Binding Path="CustomerRating"/>
                                    <Binding Path="Customername"/>
                                </MultiBinding>
                            </Setter.Value>
                        </Setter>
                    </Style.Setters>
                </Style>
            </DataGrid.RowStyle>

But my Colors are not getting reflected on grid row!!

Edit 1:

Background is of type Brush ,then How is the following Code fine ?? This Works as expected though returning string !!!

    class ColorConverter:IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                int colorValue = Int32.Parse(value.ToString());

                if (colorValue < 7)
                {
                    return "Blue";
                }
                return "Red";
            }

            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                throw new NotImplementedException();
            }
        }

<Setter Property="Background" Value="{Binding CustomerRating,Converter={StaticResource colorConverter}}">
Was it helpful?

Solution

Background is of type Brush but you are returning String from converter. Return brush instance instead:

return new SolidColorBrush(Colors.Blue);

Replace all other instances to return SolidColorBrush.


UPDATE

I have seen some strange corners where IValueConverter works but doesn't work with IMultiValueConverter. Multi-value converters need to return same type as that of target property.

Even when you bind Width with IValueConverter and return 100 from it, it will work fine. But try returning 100 from IMultiValueConverter, it won't work unless you changed it to 100.0 because width is of type double.

I guess with IValueConverter type conversion is handled by WPF binding engine but not the case with IMultiValueConverter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top