IValueConverter can be used if the Background property of a cell is to be set depending on the value in the cell. I have a requirement where in the background property of a cell in Field1 is to be set depeding on the difference of the corresponding values in Field2 and Field3. What is the best way to implement this.
Thanks.
Hello,
In this case, you need a MultiValueConverter. You can see an example of using IMultiValueConverter here.
Thanks for the reply. I implemented this in the similar way. In the BrushConverter class I am not getting the Values I am looking for.
For each row the background of Field3 will be set depending on the values in Field1, 2 and 3. Can you help me figure out what I am doing wrong.
<Style x:Key="BackgroundColorConverter" TargetType="{x:Type igDP:CellValuePresenter}"> <Setter Property="Background"> <Setter.Value> <MultiBinding Converter="{StaticResource ColorConverter}"> <Binding Path="DataItem.Value"/> <Binding Path="Field1" /> <Binding Path="Field2" /> </MultiBinding> </Setter.Value> </Setter> </Style>
<igDP:XamDataGrid.FieldLayouts> <igDP:FieldLayout IsDefault="True"> <igDP:FieldLayout.Fields> <igDP:Field Name="Field1" Label="Field1"> <igDP:Field.Settings> <igDP:FieldSettings AllowEdit="False" CellHeight="30" /> </igDP:Field.Settings> </igDP:Field> <igDP:Field Name="Field2" Label="Field2" > <igDP:Field.Settings> <igDP:FieldSettings AllowEdit="False" AllowGroupBy="True"/> </igDP:Field.Settings> </igDP:Field>
<igDP:Field Name="Field3" Label="Field3"> <igDP:Field.Settings> <igDP:FieldSettings AllowEdit="True" CellMaxWidth="150" CellValuePresenterStyle="{StaticResource BackgroundColorConverter}" /> </igDP:Field.Settings> </igDP:Field>
</igDP:FieldLayout.Fields> </igDP:FieldLayout> </igDP:XamDataGrid.FieldLayouts>
public class BrushConvertor: IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { int a =(int) values[0]; int b =(int) values[1]; int c =(int) values[2]; if (a > (b-c)) { return Brushes.Yellow; } else { return Brushes.Red; } }
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotImplementedException(); }
}