我遇到了WPF DataGrid的问题。
我想设置已禁用的Foreground
行的DataGrid
值,但Foreground
始终为灰色。
以下是我使用的代码:
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<EventSetter Event="MouseDoubleClick" Handler="DataGridRowDoubleClick" />
<Setter Property="IsEnabled" Value="{Binding Path=IsMD4Valid}" />
<Setter Property="IsHitTestVisible" Value="{Binding Path=IsMD4Valid}" />
<Style.Triggers>
<DataTrigger Binding="{Binding State}" Value="Added">
<Setter Property="Background" Value="DarkGreen"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="FontWeight" Value="Bold"/>
</DataTrigger>
<DataTrigger Binding="{Binding State}" Value="Changed">
<Setter Property="Background" Value="DarkBlue"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="FontWeight" Value="Bold"/>
</DataTrigger>
<DataTrigger Binding="{Binding State}" Value="Deleted">
<Setter Property="IsEnabled" Value="false" />
<Setter Property="IsHitTestVisible" Value="false" />
<Setter Property="Background" Value="DarkRed"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="FontWeight" Value="Bold"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
Added
,Changed
和Deleted
只是枚举值。
这是我得到的:
如您所见,Background
已应用,但Foreground
没有(对于Deleted
州的行
答案 0 :(得分:2)
定义自定义DataGridCell
样式:
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{Binding Foreground, RelativeSource={RelativeSource AncestorType=DataGridRow}}"/>
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>