您可以将 LoadingRow 事件处理程序添加到 DataGrid,然后在其各自项目的视图模型属性上设置到每行的 Background 属性的绑定:
XAML 数据网格:
<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);
}