你可以一个LoadingRow事件处理程序添加到您的DataGrid,然后就各自项目的视图模型属性设置一个绑定到每个行的背景属性:
XAML的DataGrid:
<data:DataGrid ItemsSource="{Binding FooBars}" LoadingRow="dataGrid_LoadingRow" >
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="FOO" Binding="{Binding Foo}" Width="200" />
<data:DataGridTextColumn Header="BAR" Binding="{Binding Bar}" Width="60"/>
</data:DataGrid.Columns>
</data:DataGrid>
代码隐藏:
private void packetsDataGrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
Binding backgroundBinding = new Binding("FooBarItemBackground");
backgroundBinding.Source = e.Row.DataContext;
e.Row.SetBinding(DataGridRow.BackgroundProperty, backgroundBinding);
}
有一个问题...例如:当我添加新行时,我必须更新下面的每一行都必须更新其背景。 – Augusto 2012-04-12 17:58:33
没问题:)当添加一个新行可能触发视图模型上的事件来更新每个项目的背景颜色属性。由于它的数据绑定,你不必重新绑定网格或任何东西。 – KodeKreachor 2012-04-12 18:00:43
这听起来像我使用的方式也应该是要走的路 – Stainedart 2012-04-12 18:23:03