我正在尝试更改 DataGridTextColumn 的颜色。
这是我正在做的:
<DataGridTextColumn
Header="Status"
Binding="{Binding IsActive,
Converter= {StaticResource BoolToStatusConverter}}"
Foreground="{Binding Path=IsActive,
Converter={StaticResource BoolToColorConverter}}"/>
文本设置正确,但颜色不会改变,我收到以下错误:
System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or
FrameworkContentElement for target element. BindingExpression:Path=IsActive;
DataItem=null; target element is 'DataGridTextColumn' (HashCode=40349079); target
property is 'Foreground' (type 'Brush')
我应该怎么做才能让它发挥作用?
最佳答案
您需要为列的 CellStyle 指定带有 DataTrigger 的样式。例如
<Page.Resources>
<Style TargetType="DataGridCell" x:Key="ActiveCellStyle">
<Setter Property="Foreground" Value="Blue"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsActive}" Value="{x:Null}">
<Setter Property="Foreground" Value="Green"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsActive}" Value="True">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
<Converters:BoolToTextConverter
x:Key="BoolToStatusConverter"
TargetCondition="True"
IsMatchValue="It's active"
IsNotMatchValue="It's dead" />
</Page.Resources>
<Grid>
<DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn
Header="Status"
Binding="{Binding IsActive,
Converter={StaticResource BoolToStatusConverter}}"
CellStyle="{StaticResource ActiveCellStyle}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
关于c# - 如何设置DataGridTextColumn的文字颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10950904/