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);
}

3 Replies

MS Malini Selvarasu Syncfusion Team August 13, 2024 01:59 PM UTC

Hi Álvaro Sánchez López,

Query 1: How to Use Different Properties within the Same Converter?
Based on the information provided, we understand that you need to utilize different properties within the same converter. This can be achieved by checking the `targetType` and updating the properties accordingly. We have prepared a sample to demonstrate this approach and have attached it for your review. Please examine the provided sample and code snippet, and let us know if you have any further questions.
Code  Snippet:
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;
  }

if we have misunderstood your requirement, kindly provide more details so that we can assist you further.

Query 2: SelectionBackground Not Applied to Selected Cells When GridCell's Background is Customized
Regarding the issue, we have created a new forum. Please follow the forum link below for further updates:
Regards,
Malini Selvarasu

Attachment: SfDataGrid_Demo_8e597c6d.zip


ÁS Álvaro Sánchez López August 13, 2024 02:06 PM UTC

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 .



MS Malini Selvarasu Syncfusion Team August 14, 2024 02:15 PM UTC

Hi Álvaro Sánchez López,

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.
If you have any concerns or require further assistance, please do not hesitate to contact us. We are here to help.
Regards,
Malini Selvarasu

Loader.
Up arrow icon