本文介绍了DataGrid行背景色MVVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用MVVM体系结构,我想更改数据网格中的行颜色。 行的颜色取决于模型中的项目。
Im using MVVM architecture and I want to change the row color in a datagrid. The color of row depends on the item from the model.
到目前为止,我有以下代码:
so far I have this Code:
private void DataGrid_LoadingRow(object sender, DataGridRowEventArgs e) { Log4NetLog dataGridRow = e.Row.DataContext as Log4NetLog; if (highlight) { if (dataGridRow != null) { e.Row.Background = new SolidColorBrush( dataGridRow.LogColour.Colour); } } else { e.Row.Background = new SolidColorBrush(Colors.White); } }如您所见,在第二行中引用模型中的 Log4NetLog 。
As you can see, in the second Line I have to make an reference to a Log4NetLog which is in the model.
因此,如何更改代码以适应MVVM模式?
So how can I change the code to adapt the MVVM pattern?
推荐答案我假设您的DataGrids ItemsSource绑定到Log4NetLog的集合,因此可以在xaml中进行样式设置:
I assume your DataGrids ItemsSource is bound to an Collection of Log4NetLog's, so you can do styling in xaml:
<DataGrid.ItemContainerStyle> <Style TargetType="{x:Type DataGridRow}"> <Setter Property="Background" Value="{Binding Path=LogColour.Colour}"/> </Style> </DataGrid.ItemContainerStyle>也许您需要使用颜色到SolidColorBrush Converter。
Maybe you need a Color to SolidColorBrush Converter.
更多推荐
DataGrid行背景色MVVM
发布评论