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.
+2
A:
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));
}
}
Simon Steele
2008-12-18 15:23:37
this seems like it makes sense over a valueconvertor for changing the color of the entire row. is that right?
Simon_Weaver
2009-12-11 04:36:07
Note: LoadingRow is the new name for PreparingRow
Simon_Weaver
2009-12-11 04:40:04
+1
A:
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
A:
I've generally written custom ValueConverters for each data type being bound that return Visibility, Colour, etc.
This gives a single point where the customisation rules are defined and I've found works very well.
Robin's second link describes writing a custom ValueConverter.
Rammesses
2009-01-23 15:16:13