I need to change Background of cell with MultiBinding Converter. How to add ItemSource to Binding?
My code :
Style TargetType="Syncfusion:GridCell">
<Setter Property="Background">
<Setter.Value>
<MultiBinding Converter="{StaticResource myValidConverter}">
<Binding Path="ItemSource" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type Syncfusion:SfDataGrid}}"/>
<Binding RelativeSource="{RelativeSource Self}"/>
<Binding/>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
But in Converter values[0] = DependencyProperty.UnsetValue
Hi Ishanin Vladimir Alexandrovich,
Your requirement to pass the ItemSource as a parameter in the MultiBinding converter can
be achieved by passing the ItemSource property of SfDataGrid. Please refer to the below
code snippet,
XAML Code Snippet:
|
<Window.Resources> <local:ColorConverter x:Key="converter"/> <Style TargetType="syncfusion:GridCell" > <Setter Property="Background" > <Setter.Value> <MultiBinding Converter="{StaticResource converter}" > <Binding Path="ItemsSource" ElementName="sfDataGrid" /> <Binding ElementName="sfDataGrid" /> <Binding RelativeSource="{RelativeSource Self}" /> </MultiBinding> </Setter.Value> </Setter> </Style> </Window.Resources>
<Grid> <syncfusion:SfDataGrid x:Name="sfDataGrid" AllowEditing="True" ItemsSource="{Binding Orders}" AutoGenerateColumns="True"> </syncfusion:SfDataGrid> </Grid> |
C# Code Snippet:
|
public class ColorConverter : IMultiValueConverter { public object Convert(object[] value, Type targetType, object parameter, CultureInfo culture) { //here get the ItemsSource var getItemsSource = value[0];
//here get the SfDataGrid var dataGrid = value[1] as SfDataGrid; //another way to get the ItemsSource var ItemsSource = dataGrid.ItemsSource;
//here get the GridCell var gridcell = value[2] as GridCell;
//here customize based on your scenario
//Check the condition based on cell value for different column
return new SolidColorBrush(Colors.LightBlue);
}
object[] IMultiValueConverter.ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } |
UG Link: https://help.syncfusion.com/wpf/datagrid/conditional-styling?cs-save-lang=1&cs-lang=xaml#conditional-styling-of-cells-using-converter
https://help.syncfusion.com/wpf/datagrid/conditional-styling?cs-save-lang=1&cs-lang=xaml#cell-style
https://help.syncfusion.com/wpf/datagrid/styles-and-templates
KB Link: https://www.syncfusion.com/kb/4186/how-to-change-the-gridcell-style-at-runtime
Please find the sample in the attachment and let us know if you have any concerns
in this.
Regards,
Vijayarasan S