【发布时间】:2015-12-04 10:37:38
【问题描述】:
当该行中的条件发生时,我正在尝试更改 DataGrid 行颜色后面的代码。比如:
private void Datagrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
if((((System.Data.DataRowView)(e.Row.DataContext)).Row.ItemArray[3].ToString()) == "ERROR")
e.Row.Background = new SolidColorBrush(Colors.Red);
else
e.Row.Background = null;
}
这很简单,但它不起作用:
我在
if行收到InvalidCastException。-
即使我写了一行:
e.Row.Background = new SolidColorBrush(Colors.Red);...它不起作用,没有一条线变红。
---添加---
1 对于第一个问题,错误来自以下演员表
(System.Data.DataRowView)(e.Row.DataContext))
e 来自哪里
private void Datagrid_LoadingRow(object sender, DataGridRowEventArgs e)
所以事件被正确调用,但我无法让行/列项以编程方式更改背景或前景。
- 我无法更改背景,但可以更改前景。如果你看一下下面的图片,你会发现这些行有不同的颜色。我没有设置任何目的,xaml 中的数据网格定义也没有任何特别之处。但这就是我无法设置背景的原因。我怎样才能撤消这种情况?
XAML -
<DataGrid x:Name="dtgEventsPCDmis" Grid.Row="6" FontSize="12" Background="{x:Null}" BorderBrush="Gainsboro" VerticalContentAlignment="Stretch" BorderThickness="5" Margin="10,21.4,9.6,0" LoadingRow="Datagrid_LoadingRow" AutoGeneratingColumn="Datagrid_AutoGeneratingColumn" VerticalAlignment="Top" Height="139" RenderTransformOrigin="0.5,0.5" GridLinesVisibility="All"/>
【问题讨论】:
标签: c# wpf datagrid background row