2012-04-05 62 views
1

我想根据单元格中包含的值更改单元格的文本颜色 我使用值转换器,但不知何故传递给Convert函数的对象的类型是DataRowView,我想通过这个单元格,因为我想根据它的值一次点亮一个单元格。希望这是有道理的。wpf为DataGrid中的单元格着色基于值

谢谢!

代码如果我申请到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 { #地区的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 
} 

回答

2

如果您创建适用于每种控件的样式与该类型匹配。

只需使用DataGridTemplateColumn并根据需要创建您可以执行的自定义模板。

<DataGridTemplateColumn Header="Name">      
     <DataGridTemplateColumn.CellTemplate> 
        <DataTemplate> 
         <TextBlock Foreground="{Binding Name,Converter={StaticResource colconverter}}" Text="{Binding Name}" /> 
        </DataTemplate> 
       </DataGridTemplateColumn.CellTemplate> 
    </DataGridTemplateColumn> 
+0

我的网格绑定到DataTable,所以我没有自己定义任何列。我在哪里粘贴此代码? – Anya 2012-04-05 09:59:12

+0

如果您知道哪些列正在联机到您的源行,那么您可以将它与上面的相同。例如你有员工datatable和名称是列,那么上述将工作正常。 – JSJ 2012-04-05 10:53:35

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> 

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

+0

谢谢,我会尝试 – Anya 2012-04-05 09:55:13

+0

我的网格绑定到DataTable,所以我没有自己定义任何列。我在哪里粘贴此代码? – Anya 2012-04-05 10:02:22

相关问题