Hi,
We are using XAMGrid and we want to change background color of some of grid rows as per some conditions. Suppose I have three Column Name, Dob, Address. Now I will compare DOB with current date If It will be greater than 18 years In this scenario this particular row background color will be change in Yellow Color. If It will be less than 15 Years In this scenario this particular row background color will be change in Yellow Color.
Please let us aware. How we can approach it. and here We are using XAMGRID.
Thanks
Hello Pri kanwar,
The XamGrid has a property named IsAlternateRowsEnabled which applies a separate color to odd-indexed rows in the grid, and in this case will ignore your Style for CellControl. This property defaults to "True," and so the alternate rows are enabled at the start, which is why you are seeing the behavior you are seeing. If you set this to false, the CellControl style behavior that you are applying should be consistent across all rows in your grid.
In the case that you do not want to disable alternate row styling in the grid, you will need to pull in the default style for CellControl and modify it. This style can be found in the generic.xaml file commonly found at the following directory:
C:\Program Files (x86)\Infragistics\<your version here>\WPF\DefaultStyles\XamGrid
After including it, I would recommend commenting out the two VisualStates for MouseOver and Alternate, as these will apply the alternate styling. After doing that, you should locate a Rectangle named "AltMouseOver." This Rectangle is the alternatively colored row, and I would recommend setting its Fill property to {StaticResource CellItemAltNormalBackgroundBrush}, as this is the normal fill for the alternate rows, and that resource is necessary to pull in from the generic.xaml file for this default style to take effect. Finally, I would recommend adding your DataTriggers to the ControlTemplate.Triggers collection so that you can target both the Background property and the Fill property of the "AltMouseOver" Rectangle with your setters. This trigger would look like the following:
<DataTrigger Binding="{Binding Birthday, Converter={StaticResource conv}}" Value="True"> <Setter Property="Background" Value="Yellow"/> <Setter TargetName="AltMouseOver" Property="Fill" Value="Yellow"/></DataTrigger>
I have attached a modified version of the original sample project I had sent you to demonstrate the default style procedure mentioned above. In the sample, the default style exists in the App.xaml file.
Please let me know if you have any other questions or concerns on this matter.
Sincerely,AndrewAssociate Developer
Thanks for your support. We have added multiple DataTrigger like below.
<DataTrigger Binding="{Binding ExpirationDate, Converter={StaticResource expireDateConv}}" Value="10"> <Setter Property="Background" Value="Yellow"/> </DataTrigger>
<DataTrigger Binding="{Binding ExpirationDate, Converter={StaticResource expireDateConv}}" Value="20"><Setter Property="Background" Value="Red"/></DataTrigger>
But after applying this style grid is changing alternate color. If one row get value 10 It will not change the color, but the next row get value 10 will change in yellow color.
Please guide us where we are going to wrong.
Yes, you should add multiple DataTriggers with multiple values like you had explained above. If one DataTrigger fails, it will move to the next until it finds one that has a correct value. If none of the values are correct, the default behavior will continue.
Thanks for your reply, But I am already aware about converter that can pass multiple value or any kind of values. But only just wish to know how we can set multiple colors with different values.
<DataTrigger Binding="{Binding ExpirationDate, Converter={StaticResource expireDateConv}}" Value="20"> <Setter Property="Background" Value="Yellow"/></DataTrigger>
Above style will change Row color Yellow, If value will be 20. If I get value 30 then background color should be Green or If I get value 40 then background color should be Red. Should I add multiple data trigger with multiple values for that like below. Or something else ?
<DataTrigger Binding="{Binding ExpirationDate, Converter={StaticResource expireDateConv}}" Value="30"> <Setter Property="Background" Value="Green"/></DataTrigger>
<DataTrigger Binding="{Binding ExpirationDate, Converter={StaticResource expireDateConv}}" Value="40"> <Setter Property="Background" Value="Red"/></DataTrigger>
The converter in this case is what is returning the values of True and False, which is checked in the DataTrigger, but the converter does not have to return a bool type. You can return essentially anything, and as long as the return type is in a format that can be checked by the DataTrigger, you can have as many conditions as you would like.
For example, rather than returning "True" for Age 20, you could return "20" from the converter. At that point, your DataTrigger would look like: