We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Change the Background of a row dynamically

Hi,

i am using the conditional styling approach using your samples at: https://help.syncfusion.com/wpf/datagrid/conditional-styling#row-style
My Converter is called and i am setting rows.

The content of the row is bound to a collection in my ViewModel and when i change some value, the row color should be checked.
However the color converter is only called, when the grid is initally displayed.
The grid in my MVVM view seems not to recognize, that the bound column has been changed and the converter is not called.

Note: that i see the changed values in the grid appear. so the binding is ok.

Any ideas?

thanks,

Helmut

4 Replies

HW Helmut Wahrmann November 24, 2019 09:17 AM UTC

Ok, i found it using the hint in your KB article: https://www.syncfusion.com/kb/6658/how-to-change-row-background-based-on-record

I set the Path and now it works:

    <Style TargetType="syncfusion:VirtualizingCellsControl" x:Key="changedRowStyle">
      <Setter Property="Background" Value="{Binding Path=Changed,UpdateSourceTrigger=PropertyChanged,Converter={StaticResource changedRowStyleSelector}}"/>
    </Style>

Only drawback now is that i need to have a second ColorConverter, since i am using also an alternate roswstyle and the colorconverter is bound to rowstyle, which is only called for odd rows.


MA Mohanram Anbukkarasu Syncfusion Team November 25, 2019 12:16 PM UTC

Hi Helmut,   
  
Thanks for contacting Syncfusion support.    
  
The converter bound to the RowStyle will be called only for the odd rows as you specified when alternative row style is applied. You need to create another converter and bind it to the AlternativeRowStyle as specified in the below code example.   
  
Code example:    
  
XAML:    
  
<Window.Resources>   
    <local:CustomRowStyleConverter x:Key="CustomRowStyleConverter" />   
    <local:AlternativeRowStyleConverter x:Key="AlternativeRowStyleConverter" />   
    <Style x:Key="rowStyle" TargetType="Syncfusion:VirtualizingCellsControl">   
        <Setter Property="Background" Value="{Binding Converter={StaticResource CustomRowStyleConverter}, UpdateSourceTrigger=PropertyChanged}" />   
    </Style>   
    <Style x:Key="alternativeRowStyle" TargetType="Syncfusion:VirtualizingCellsControl">   
        <Setter Property="Background" Value="{Binding Converter={StaticResource AlternativeRowStyleConverter},UpdateSourceTrigger=PropertyChanged}"/>   
    </Style>   
</Window.Resources>   
   
<Syncfusion:SfDataGrid x:Name="datagrid"   
                       AllowEditing="True"   
                       ItemsSource="{Binding ItemsCollection}"   
                       RowStyle="{StaticResource rowStyle}"   
                       AlternatingRowStyle="{StaticResource alternativeRowStyle}" />   
  
  
CS:    
  
public class CustomRowStyleConverter : IValueConverter   
{   
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)   
    {   
        if (value == null)   
            return DependencyProperty.UnsetValue;   
   
        if ((value as BusinessObjects).IsChecked)   
            return new SolidColorBrush(Colors.Green) { Opacity = 0.7 };   
        return new SolidColorBrush(Colors.AliceBlue);   
    }   
   
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo cultur   
    {   
        throw new NotImplementedException();   
    }   
}   
   
public class AlternativeRowStyleConverter : IValueConverter   
{   
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)   
    {   
        if (value == null)   
            return DependencyProperty.UnsetValue;   
   
        if ((value as BusinessObjects).IsChecked)   
            return new SolidColorBrush(Colors.Green) { Opacity = 0.7 };   
        return new SolidColorBrush(Colors.Beige);   
    }   
   
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo cultur   
    {   
        throw new NotImplementedException();   
    }   
}   
  
  
  
Please let us know if you require further other assistance from us.    
  
Regards,   
Mohanram A  



HW Helmut Wahrmann November 27, 2019 08:13 PM UTC

That's exactly what i did.
Thanks for the confirmation


FP Farjana Parveen Ayubb Syncfusion Team November 28, 2019 05:48 AM UTC

Hi Helmut, 
 
Thanks for the update. 
 
We are glad to know that the reported problem has been resolved at your end. Please let us know if you have any further queries on this. We are happy to help you. 
 
Regards, 
Farjana Parveen A 


Loader.
Live Chat Icon For mobile
Up arrow icon