2014-04-25 60 views
0

一旦设置了数据绑定,我该如何根据列中设置的值来设置datagrid的每一行样式?我会使用后面的代码或使用XAML样式触发器来设计它吗?样式Datagrid

我现在的问题是,它将取决于列[0]中设置的值。

说列[0]的值是1,2,3。我想根据这个值设置datagrid的行。

例如

c# 
    DataGrid1.ItemsSource = schDT.DefaultView 

    xaml 
    <DataGridTextColumn Binding="{Binding ITEMNUMBER}" Header="ITEMNUMBER" /> 
    <DataGridTextColumn Binding="{Binding CODE}" Header="CODE" /> 
    <DataGridTextColumn Binding="{Binding DESC}" Header="STD DESCRIPTION" /> 

感谢您的帮助一如既往

+0

http://stackoverflow.com/questions/18053281/how-to-set-datagrids-row-background-based-on-a-property-value-using-data-bindi – ullevi83

回答

0

这是一个很讨厌的问题,我不得不前解决一些时间。 我使用了以下代码:

基本上:您需要一个Value转换器来获得行/单元格的值。 在这里的例子中,我使用了一个多值转换器,因为我也需要关于Colzumn的信息 - 取决于你的场景,一个正常的值转换器就足够了。

XAML //用于convers

<DataGrid x:Name="bla"> 
    // use this column if you want to style the header too 
    <DataGrid.RowHeaderStyle> 
     <Style TargetType="DataGridRowHeader"> 
      <Setter Property="Background" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, 
       AncestorType={x:Type DataGridRow}}, 
       Converter={StaticResource ExcelRowColorConverter}}"> 
      </Setter> 
      <Setter Property="BorderThickness" Value="1,0,1,1"></Setter> 
      <Setter Property="BorderBrush" Value="#FF000000"></Setter> 
     </Style> 
    </DataGrid.RowHeaderStyle> 
    <DataGrid.CellStyle> 
     <Style TargetType="{x:Type DataGridCell}"> 
      <Setter Property="DataGrid.Background"> 
       <Setter.Value> 
        <MultiBinding Converter="{StaticResource ExcelDataGridCellColorConverter}" > 
         <MultiBinding.Bindings> 

//这是临界片样式特定细胞

的C#-Code

//----------------------------------------------------------------------- 
// <copyright file="ExcelDataGridCellColorConverter.cs" company="best Research"> 
//  Copyright (c) best Research. All rights reserved. 
// </copyright> 
//----------------------------------------------------------------------- 

namespace myTool.Utils 
{ 
    using System; 
    using System.Collections.Generic; 
    using System.Data; 
    using System.Globalization; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    using System.Windows.Controls; 
    using System.Windows.Data; 
    using System.Windows.Media; 

    /// <summary> 
    /// Used to convert a cell to its final color. It determines this based on both row and column. 
    /// </summary> 
    public class ExcelDataGridCellColorConverter : IMultiValueConverter 
    { 
     /// <summary> 
     /// Used to convert a cell to its final color. It determines this based on both row and column. 
     /// </summary> 
     /// <param name="values">the parameter array</param> 
     /// <param name="targetType">The target type.</param> 
     /// <param name="parameter">the parameter.</param> 
     /// <param name="culture">the culture.</param> 
     /// <returns> 
     /// A Brush 
     /// </returns> 
     public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
     { 
      if (values[1] is DataRow) 
      { 
       var cell = (DataGridCell)values[0]; 
       var row = (DataRow)values[1]; 
       var columnName = cell.Column.SortMemberPath; 
       var table = row.Table; 

       // this is used because there is a bug in WPF which causes a removed datarow to be "seen". This happens when deleting empty rows. The Key "lock" should be added by the deleting method and then removed afterwards. 
       if (table.ExtendedProperties.Contains("lock")) 
       { 
        return Brushes.Gainsboro; 
       } 

       var colValue = table.Rows[table.Rows.Count - 1][columnName]; 
       var rowValue = row[Properties.Resources.ColorColumnName]; 
       var colorColValue = (Enums.RowState)Enum.Parse(typeof(Enums.RowState), colValue.ToString(), true); 
       var colorRowValue = (Enums.RowState)Enum.Parse(typeof(Enums.RowState), rowValue.ToString(), true); 

       if (colorColValue == Enums.RowState.IsIncluded && colorRowValue == Enums.RowState.IsIncluded) 
       { 
        return Brushes.LightGreen; 
       } 
       else if (colorRowValue == Enums.RowState.HeaderRow) 
       { 
        return Brushes.Gainsboro; 
       } 
       else 
       { 
        return Brushes.LightSalmon; 
       } 
      } 

      return Brushes.Blue; 
     } 

     /// <summary> 
     /// Not used 
     /// </summary> 
     /// <param name="value">the parameter</param> 
     /// <param name="targetTypes">The target types.</param> 
     /// <param name="parameter">the parameter.</param> 
     /// <param name="culture">the culture.</param> 
     /// <returns> 
     /// A Brush 
     /// </returns> 
     public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
     { 
      throw new System.NotImplementedException(); 
     } 
    } 
} 
+0

感谢基督教,这是我的另一种选择,我找到了我正在寻找的答案(见上文)。感谢您花时间回答我的问题。 – ullevi83