DataGrid行背景色MVVM

编程入门 行业动态 更新时间:2024-10-27 08:33:54
本文介绍了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

本文发布于:2023-11-05 04:24:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1559892.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:背景色   DataGrid   MVVM

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!