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(); }
}
I am attempting similar fate, but not having 100% success. The data binding path works and i get the correct values in the converter (and send the correct back), however, I never see them assigned to the cells in my grid.
I've tried removing every editor and existing format possible to see if there is an over-ride somewhere. I've found none.
Please view my code and let me know if you see any issues.
Thanks - Glenn
The Style:
<Style x:Key="compareRegMWValues" TargetType="{x:Type igDP:CellValuePresenter}"> <Setter Property="Background"> <Setter.Value> <MultiBinding Converter="{StaticResource CompareRegMWValues}"> <Binding Path="Record.Cells[CurregMW].Value" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type igDP:DataRecordPresenter}}"/> <Binding Path="Record.Cells[RegMW].Value" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type igDP:DataRecordPresenter}}"/> </MultiBinding> </Setter.Value> </Setter> </Style>
The Field:
<igDP:Field Name="RegAssignDelta" Label="Reg Assign Delta"> <igDP:Field.Settings> <igDP:FieldSettings AllowEdit="False" CellWidth="70" LabelWidth="70" CellValuePresenterStyle="{StaticResource compareRegMWValues}" LabelPresenterStyle="{DynamicResource GridLabelPresenterStyle}" LabelTextAlignment="Center" AllowSummaries="True" SummaryUIType="MultiSelect"> <igDP:FieldSettings.EditorStyle> <Style TargetType="{x:Type igEditors:XamNumericEditor}" > <Setter Property="Format" Value="######0.0"/> </Style> </igDP:FieldSettings.EditorStyle> </igDP:FieldSettings> </igDP:Field.Settings> </igDP:Field>
The Converter:
public object Convert(object[] values, Type targetType,object parameter, CultureInfo culture) { decimal value1 = 0; decimal value2 = 0;
if (values[0] == null || values[1] == null) { return Binding.DoNothing; }
// If Current RegMW > Reg MW Then Blue value1 = System.Convert.ToDecimal(values[0]); value2 = System.Convert.ToDecimal(values[1]);
//if ((values[0] is int && values[1] is int) && (int)values[0] > (int)values[1]) if (value1 > value2) { return Brushes.Blue; } // If Current RegMW < Reg MW Then Blue //else if ((values[0] is int && values[1] is int) && (int)(values[0]) < (int)(values[1])) else if (value1 < value2) { return Brushes.Red; } else { return Binding.DoNothing;} }