SfDataGrid Cell Styles (with Converters) for Selected cells not visible
Hi,
I am using converters for Cell styling and I need to use my view model in the converter class.
I have tried to use the "ConverterParameter" but it does not work.
Note that I am not declaring the converter for each column since my columns are created programmatically (I work with dynamic binding) so I have this in the xaml.
The Convert method get the GridCell perfectly, but I also need to use the ViewModel there.
Some questions about the styling using converters:
- Is possible to handle different properties (Background, Font, etc.) at the same time? Is a bit annoying for me to handle different converters or the same but different times for each cell.
- After I handle the cell background (Background property) the Selected style (CellSelectionBrush), that was (gray by default), is not visible anymore. So I am not able to difference when I am selecting cells or not.
It is related with the opacity of the brush in the cell background but I do not want to reduce the opacity for the cell background. Is there any solution for that? Try to determine if the cell that is being painting is selected is not a solution for me because of the performance.
<Grid.Resources>
<ResourceDictionary>
<converters:CellSelectedStyleConverter x:Key="cellSelectedStyleConverter"/>
<converters:CellBackgroundStyleConverter x:Key="cellBackgroundStyleConverter"/>
<converters:CellFontStyleConverter x:Key="cellFontStyleConverter"/>
<Style TargetType="syncfusion:GridCell">
<Setter Property="CellSelectionBrush"
Value="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource cellSelectedStyleConverter}}"/>
<Setter Property="Background">
<Setter.Value>
<MultiBinding Converter="{StaticResource cellBackgroundStyleConverter}">
<Binding Path="DataContext" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=UserControl}"/>
<Binding Path="DataContext" RelativeSource="{RelativeSource Mode=Self}"/>
<Binding Path="ColumnBase.GridColumn.MappingName" RelativeSource="{RelativeSource Mode=Self}"/>
</MultiBinding>
</Setter.Value>
</Setter>
<Setter Property="FontWeight">
<Setter.Value>
<MultiBinding Converter="{StaticResource cellFontStyleConverter}">
<Binding Path="DataContext" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=UserControl}"/>
<Binding Path="DataContext" RelativeSource="{RelativeSource Mode=Self}"/>
<Binding Path="ColumnBase.GridColumn.MappingName" RelativeSource="{RelativeSource Mode=Self}"/>
</MultiBinding>
</Setter.Value>
</Setter>
<Setter Property="Foreground">
<Setter.Value>
<MultiBinding Converter="{StaticResource cellFontStyleConverter}">
<Binding Path="DataContext" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=UserControl}"/>
<Binding Path="DataContext" RelativeSource="{RelativeSource Mode=Self}"/>
<Binding Path="ColumnBase.GridColumn.MappingName" RelativeSource="{RelativeSource Mode=Self}"/>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
</Grid.Resources>
The Convert methods are like those:
// CellBackground converter
public override object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values[0] is MyCustomViewModel viewModel
&& values[1] is DynamicDictionary data
&& values[2] is string column)
{
// Some buissness logic for determine the cellStyle
var cellStyle = GetMyStyle(...);
return new SolidColorBrush(cellStyle.GetBackColor());
}
return DependencyProperty.UnsetValue;
}
// Cell Selected Converter
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return new SolidColorBrush(Colors.LightGray);
}
Query 1: How to Use Different Properties within the Same Converter?
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { if (targetType == typeof(Brush)) { return new SolidColorBrush(Colors.LightBlue) ; } else if (targetType == typeof(double)) { return 15.0; } else if(targetType == typeof(FontWeight)) { return FontWeights.Bold; } return DependencyProperty.UnsetValue; } |
Query 2: SelectionBackground Not Applied to Selected Cells When GridCell's Background is Customized
Malini Selvarasu
Attachment: SfDataGrid_Demo_8e597c6d.zip
Hi Malini,
Thanks for the reply.
Regarding the query 1. I would like to handle all the properties (background, font, weight, etc.) at the same time. So the Convert method is reached only one time. Maybe, with a list of targetTypes (¿?).
I am currently using your approach, but as you know the Convert methods is reached one time per targetType .
Upon reviewing your scenario, we understand that you intend to invoke the converter once for multiple target types. However, this approach is not feasible. When binding multiple properties (such as Background, FontSize, and FontWeight) of a control to the same converter, the Convert method is called separately for each property. Therefore, using the same converter for multiple target properties results in multiple invocations, one for each property.
Malini Selvarasu
- 3 Replies
- 2 Participants
-
ÁS Álvaro Sánchez López
- Aug 12, 2024 07:26 AM UTC
- Aug 14, 2024 02:15 PM UTC