Hi Guys,
I have a table with some common ids. Is it possible to color the background of rows with similar values with specific color. I don't want them colored if the row has no common value with any other row in the table?
Hello Ashutosh,
In order to color the background of your XamDataGrid rows based on similar values, I would recommend writing a Style for DataRecordPresenter and using a DataTrigger (or MultiDataTrigger, depending on the number of conditions) to bind to your underlying data item values. The data context of the DataRecordPresenter is the underlying DataRecord that it presents, which has a DataItem property that returns your underlying data item. So, if you bind to "{Binding DataItem.PropertyName}," in the DataTrigger you can check the underlying value and set the Background of the DataRecordPresenter as such. A sample DataTrigger for this could look like the following:
<DataTrigger Binding="{Binding DataItem.ID}" Value="1"> <Setter Property="Background" Value="Red"/></DataTrigger>
I have attached a sample project to demonstrate the above. I hope this helps you.
Please let me know if you have any other questions or concerns on this matter.
Sincerely,AndrewAssociate Developer
Hi Andrew,
What I meant was I have two rows with a column say ID with same value which is a variable and not constant. Is there a way to change the background color of these two rows (both should have the same color) having the same value for column ID?
In the case that the "common value" column is a variable and not constant, I would recommend creating a property on your data item that designates what color the row should be. Doing this will allow you to simply define a Setter in your DataRecordPresenter style that binds to {Binding DataItem.RowBackgroundColor}, where RowBackgroundColor is a Brush property on the underlying data item.
This would allow you to handle the coloring of your rows in the ViewModel, or perhaps on the DataValueChanged event of the XamDataGrid, as you could search for an item with a common value to the newly changed value, and assign it a color.
I have attached a modified version of the sample project I had originally sent you that includes a Behavior<XamDataGrid> that handles the DataValueChanged event and toggles the colors of the rows of the grid if duplicate values in the ID column exist after editing. I hope this helps.