1

我想根据单元格包含的值更改单元格的文本颜色我使用值转换器,但不知何故,传递给 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
}
4

2 回答 2

2

如果您创建将应用于每个控件的样式匹配该类型。

只需使用 DataGridTemplateColumn 并创建您可以随心所欲的自定义模板。

  <DataGridTemplateColumn Header="Name">                       
      <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Foreground="{Binding Name,Converter={StaticResource colconverter}}" Text="{Binding Name}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
   </DataGridTemplateColumn>
于 2012-04-05T09:23:16.657 回答
1

如果您将 a 传递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>

传递给转换器的类型就是绑定路径中的属性类型(当然,在转换之后)。

于 2012-04-05T09:38:46.580 回答