我想根据单元格包含的值更改单元格的文本颜色 我使用值转换器,但不知何故,传递给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#部分:
public class 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
}
答案 0 :(得分:2)
如果您创建的样式将应用于与该类型匹配的每个控件。
只需使用DataGridTemplateColumn并创建您可以随意执行的自定义模板。
<DataGridTemplateColumn Header="Name">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Foreground="{Binding Name,Converter={StaticResource colconverter}}" Text="{Binding Name}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
答案 1 :(得分:1)
如果您将Path
传递给需要在Binding
中转换的属性,则此方法有效。
<DataGridTextColumn Binding="{Binding Path=AgentUtilization, StringFormat=P}" Header="Agent Utilization">
<DataGridTextColumn.CellStyle>
<Style TargetType="DataGridCell">
<Setter Property="Foreground" Value="{Binding Path=AgentUtilization, Converter={StaticResource UtilizationFormat}}" />
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
传递给转换器的类型是绑定路径中的属性类型(当然,在转换后)。