Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silverlight Datagrid: Changing cell styles, based on values

I have some data. I want to go through that data and change cells (for example - Background color), if that data meets a certain condition. Somehow, I've not been able to figure it out how to do this seemingly easy thing in Silverlight.

like image 893
Mindaugas Mozūras Avatar asked Dec 13 '25 22:12

Mindaugas Mozūras


2 Answers

This is slightly old code (from before RTM), but does something like what you're looking for. It checks some data on an object in a row and then sets the colour of the row 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>

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));
    }
}
like image 171
Simon Steele Avatar answered Dec 15 '25 15:12

Simon Steele


Actually this won't work in all examples. See these links for the 'proper' way of achieving this

http://silverlight.net/forums/p/27465/93474.aspx#93474

http://silverlight.net/forums/t/27467.aspx


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!