无法设置 WPF Datagrid 列前景
WPF Datagrid column foreground cannot be set
我想将 wpf 数据网格中列的前景设置为绿色。我试过这种方式。但它不起作用。
<dg:DataGrid.Columns>
<dg:DataGridTextColumn Header="Customer" Binding="{Binding CustomerName}" Foreground="{Binding FontColor, Mode=OneWay}" Width="300" "/>
并且在模型 class 中,我已经这样指定了。
private Brush _fontcolor;
public Brush FontColor
{
get
{
return _fontcolor;
}
set
{
_fontcolor = value;
OnPropertyChanged("FontColor");
}
}
在我填充数据的视图模型 class 中,我已经这样提到了。
NotifyItem ni = new NotifyItem();
ni.CustomerID = (int)dr["CustomerID"];
ni.FontColor = Brushes.Green;
NotifyCollections.Add(ni);
这里有什么问题吗?为什么前景没有设置为绿色?有什么方法可以在不转到 DataGridTemplateColumn 的情况下将前景设置或绑定到颜色?此外,我已经为所选行设置了背景样式。下面是 xaml.
<dg:DataGrid.Resources>
<LinearGradientBrush x:Key="jj" EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF4D77F9" Offset="0"/>
<GradientStop Color="#FF96B5FF" Offset="1"/>
</LinearGradientBrush>
<Style TargetType="{x:Type dg:DataGridCell}">
<Style.Triggers>
<Trigger Property="dg:DataGridCell.IsSelected" Value="True">
<Setter Property="Background" Value="{StaticResource jj}" />
</Trigger>
</Style.Triggers>
</Style>
</dg:DataGrid.Resources>
Is there any way to set or bind the foreground to a color without going to DataGridTemplateColumn
?
是的。使用 ElementStyle
:
<DataGridTextColumn Header="Customer" Binding="{Binding CustomerName}" Width="300">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="{Binding FontColor}" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
我想将 wpf 数据网格中列的前景设置为绿色。我试过这种方式。但它不起作用。
<dg:DataGrid.Columns>
<dg:DataGridTextColumn Header="Customer" Binding="{Binding CustomerName}" Foreground="{Binding FontColor, Mode=OneWay}" Width="300" "/>
并且在模型 class 中,我已经这样指定了。
private Brush _fontcolor;
public Brush FontColor
{
get
{
return _fontcolor;
}
set
{
_fontcolor = value;
OnPropertyChanged("FontColor");
}
}
在我填充数据的视图模型 class 中,我已经这样提到了。
NotifyItem ni = new NotifyItem();
ni.CustomerID = (int)dr["CustomerID"];
ni.FontColor = Brushes.Green;
NotifyCollections.Add(ni);
这里有什么问题吗?为什么前景没有设置为绿色?有什么方法可以在不转到 DataGridTemplateColumn 的情况下将前景设置或绑定到颜色?此外,我已经为所选行设置了背景样式。下面是 xaml.
<dg:DataGrid.Resources>
<LinearGradientBrush x:Key="jj" EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF4D77F9" Offset="0"/>
<GradientStop Color="#FF96B5FF" Offset="1"/>
</LinearGradientBrush>
<Style TargetType="{x:Type dg:DataGridCell}">
<Style.Triggers>
<Trigger Property="dg:DataGridCell.IsSelected" Value="True">
<Setter Property="Background" Value="{StaticResource jj}" />
</Trigger>
</Style.Triggers>
</Style>
</dg:DataGrid.Resources>
Is there any way to set or bind the foreground to a color without going to
DataGridTemplateColumn
?
是的。使用 ElementStyle
:
<DataGridTextColumn Header="Customer" Binding="{Binding CustomerName}" Width="300">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="{Binding FontColor}" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>