Silverlight Datagrid: changing cell styles based on values

I have some data. I want to view this data and change the cells (for example, the background color) if this data meets a certain condition. Somehow, I could not figure out how to do this, it would seem, easily in Silverlight.

+3
source share
3 answers

This is a bit old code (from RTM), but does something like what you are looking for. It checks some data on the object in the string and then sets the color of the string accordingly.

XAML:

<my:DataGrid x:Name="Grid" Grid.Row="1" Margin="5" GridlinesVisibility="None" PreparingRow="Grid_PreparingRow">
    <my:DataGrid.Columns>
        <my:DataGridTextBoxColumn 
            DisplayMemberBinding="{Binding Cheese}" 
            Header="Cheese"></my:DataGridTextBoxColumn>
        <my:DataGridTextBoxColumn 
            DisplayMemberBinding="{Binding Biscuit}" 
            Header="Biscuit"></my:DataGridTextBoxColumn>
    </my:DataGrid.Columns>
</my:DataGrid>

The code:

this.Grid.AlternatingRowBackground = null; 

private void Grid_PreparingRow(object sender, DataGridRowEventArgs e)
{
    CheesyClass c = e.Row.DataContext as CheesyClass;
    if (c != null && c.Cheese == "cheddar")
    {
       e.Row.Background = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Color.FromArgb(255, 255, 125, 125));
    }
}
+5
source

I usually wrote special ValueConverters for each related data type that return visibility, color, etc.

This gives a single point where configuration rules are defined, and I found the job very well.

The second Robin link describes a custom ValueConverter entry.

0
source

All Articles