【发布时间】:2019-02-24 16:03:41
【问题描述】:
我想将 wpf 数据网格中列的前景设置为绿色。我试过这种方式。但它不起作用。
<dg:DataGrid.Columns>
<dg:DataGridTextColumn Header="Customer" Binding="{Binding CustomerName}" Foreground="{Binding FontColor, Mode=OneWay}" Width="300" "/>
并且在模型类中,我已经这样指定了。
private Brush _fontcolor;
public Brush FontColor
{
get
{
return _fontcolor;
}
set
{
_fontcolor = value;
OnPropertyChanged("FontColor");
}
}
在我填充数据的视图模型类中,我已经提到过这样的。
NotifyItem ni = new NotifyItem();
ni.CustomerID = (int)dr["CustomerID"];
ni.FontColor = Brushes.Green;
NotifyCollections.Add(ni);
这里有什么问题?为什么前景没有设置为绿色?有没有什么方法可以在不去 DataGridTemplateColumn 的情况下将前景设置或绑定到颜色?此外,我已经为所选行设置了背景样式。下面是xml。
<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>
【问题讨论】:
标签: wpf wpfdatagrid