我想根据单元格包含的值更改单元格的文本颜色我使用值转换器,但不知何故,传递给 Convert 函数的对象类型是 DataRowView,我想传递 Cell,因为我想突出显示一个根据其值一次计算单元格。希望这是有道理的。
谢谢!!
如果我适用于 DataGrid 的样式的代码:
<UserControl.Resources>
<local:MyBkColorConverter x:Key="bkColorCvrt"/>
<Style x:Key="GridStyle" TargetType="DataGrid">
<Setter Property="ItemsSource" Value="{Binding}" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="RowBackground" Value="Transparent" />
<Setter Property="HeadersVisibility" Value="None" />
<Setter Property="GridLinesVisibility" Value="None" />
<Setter Property="SelectionUnit" Value="Cell" />
<Setter Property="SelectionMode" Value="Single" />
<Setter Property="IsReadOnly" Value="True" />
<Setter Property="HorizontalScrollBarVisibility" Value="Disabled" />
<Setter Property="CellStyle">
<Setter.Value>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Foreground">
<Setter.Value>
<Binding Converter="{StaticResource bkColorCvrt}"/>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Black">
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
而 c# 部分:
公共类 MyBkColorConverter : IValueConverter { #region IValueConverter 成员
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
//The type of value here is actually DataRowView
//here i would like to have a cell passed. is that possible to archive?
return Brushes.LightGray;
}
public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}