Hello. How can I set the background color of multiple cells in a row based on the value of a separate cell in the same row? I figured out a round-about way to set the background of one cell in a row based on another cell in a row, but my solution doesn't work for setting multiple cells in a row. Here is my solution:
<xcdg:Column FieldName="." Title="Sales">
<xcdg:Column.CellContentTemplate>
<DataTemplate>
<TextBlock x:Name="txtSales" Text="{Binding Path=Sales}"/>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsTop}" Value="True">
<Setter TargetName="txtSales" Property="Background" Value="Yellow"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</xcdg:Column.CellContentTemplate>
</xcdg:Column>
I have a column called "Sales". I want the background of my "Sales" column to be yellow if the value of "IsTop" is true. This code works. It's not an ideal solution* but it works in this case
My problem is that this solution doesn't work if I try to use it with multiple columns. If I add another column with a data template similar to what I have above, I will get an exception that says that I can't add multiple columns to the column collection with the same FieldName. My solution relies on me being able to set the column's FieldName to a period (.). I can't have multiple columns with FieldNames set to a period (.).
Is there a better way to set the background color of a cell based on the value of another cell in the same row? Can I somehow use a binding to get the parent row of a cell? I tried using the Ancestor syntax, but I can't figure out how to get the code to recognize that the parent object is a DataRowView. I naively tried to set a binding path to "../IsTop" hoping that the binding uses a Unix-like syntax, but that didn't work. Thanks.
* The reason I say that this solution is not ideal is that it breaks the copy-and-paste action. If I select the whole grid and copy-and-paste it into Notepad, all of the values of my "Sales" column will be "System.Data.DataRowView" rather than the actual value of the Sales column in that row. I'm binding my datagrid to a System.Data.DataTable.