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
close icon

Dynamic Row background colour based on property from viewmodel

Hi,

I'm trying to set my Row background colour dynamically using my viewmodel. As the viewmodel value changes I want the background colour to automatically change. For example if my model has a bool property called IsActive, I want to create a binding where true would result in purple while false would result in white. Any advice?

3 Replies

SP Sowndaiyan Paulpandi Syncfusion Team October 4, 2016 12:34 PM UTC

Hi Arvin,

Thanks for contacting Syncfusion Support

In the SfDataGrid, it is possible to change the Row style at run time. You can change the Row style while changing the cell data by using  the SfDataGrid.CurrentCellEndEdit event along with the RowStyleSelector. We have prepared a sample as per your requirement and you can download the sample from the below location,

Sample : http://www.syncfusion.com/downloads/support/forum/126244/ze/UWP-330020962 

Please refer the below UG link to know more about RowStyleSelector

UG : https://help.syncfusion.com/wpf/sfdatagrid/conditional-styling#styling-rows-using-styleselector
 
Regards, 
Sowndaiyan 



PV Peter Verbo December 6, 2017 08:48 PM UTC

I have little bit different approach with using SetterValueBindingHelper:

        xmlns:utils="using:Syncfusion.UI.Xaml.Utils"

        <Style x:Key="rowStyle" TargetType="sfDataGrid:VirtualizingCellsControl">
            <Setter Property="utils:SetterValueBindingHelper.PropertyBinding">
                <Setter.Value>
                    <utils:SetterValueBindingHelper Property="Background" Binding="{Binding Converter={StaticResource DataGridCellBackgroundConverter}}"/>
                </Setter.Value>
            </Setter>
          ...
        </Style>

Converter:
    public class DataGridCellBackgroundConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string culture)
        {
            if (value is Command)
            {
                MyClass item = value as MyClass;
                return new SolidColorBrush(item.IsActive ? Colors.LightGreen : Colors.Gray);
            }
            return new SolidColorBrush(Colors.White);
        }
        public object ConvertBack(object value, Type targetType, object parameter, string culture)
        {
            throw new NotImplementedException();
        }
    }


GT Gnanasownthari Thirugnanam Syncfusion Team December 8, 2017 03:41 AM UTC

Hi Peter, 

Thank you for providing your suggested solution in Syncfusion forum. 

Please let us know if you need further assistance on this. 

Regards, 
Gnanasownthari T. 


Loader.
Live Chat Icon For mobile
Up arrow icon