Is it possible to set one or multiple rows' background color?
A:
You can use the AlternatingRowBackground
property to do every other row. Do you need to do specific rows? Like only rows 1, 5, and 9? Or different background color depending on data?
EDITED: edited per comment..
Doing it data based can be done like this...
<DataGrid
AlternatingRowBackground="AliceBlue"
ItemsSource="{Binding}"
AutoGenerateColumns="False"
Height="200"
HorizontalAlignment="Left"
Margin="156,58,0,0"
Name="dataGrid1"
VerticalAlignment="Top"
Width="200" LoadingRow="dataGrid1_LoadingRow">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding}" />
</DataGrid.Columns>
</DataGrid>
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
ObservableCollection<string> list = new ObservableCollection<string>();
list.Add("test1");
list.Add("test2");
list.Add("test3");
list.Add("test4");
list.Add("test5");
dataGrid1.ItemsSource = list;
}
private void dataGrid1_LoadingRow(object sender, DataGridRowEventArgs e)
{
if (e.Row.Item.ToString().Contains("test"))
{
e.Row.Background = Brushes.Red;
}
}
Eclipsed4utoo
2010-05-17 15:49:34
@Eclipsed4utoo: yes, different background color depending on data, is it possible? how to do that?
martin
2010-05-17 15:52:16
@Eclipsed4utoo: In my case, there are several columns, so should i use: e.Row.Item.ToString().Contains=="xxxx" ? but it doesnt work..
martin
2010-05-17 16:14:46
`Contains` is a method. so your code would be... `if (e.Row.Item.ToString().Contains("xxx")) ....
Eclipsed4utoo
2010-05-17 18:44:56
@Eclipsed4utoo: i used messagebox to display e.Row.Item.ToString(), it is nothing but rubbish...
martin
2010-05-18 08:07:13