样式化Datagrid

时间:2014-04-25 12:21:24

标签: c# wpf xaml datagrid

在设置数据绑定后,如何根据列中设置的值设置数据网格的每一行样式?我会使用后面的代码或使用XAML样式触发器设置样式吗?

我遇到的问题是,它将取决于列[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" />

一如既往地感谢您的帮助

2 个答案:

答案 0 :(得分:0)

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

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

XAML //用于转换                            

<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();
        }
    }
}

答案 1 :(得分:0)

找到答案,抱歉在发布问题之前必须更加努力。

How to set DataGrid's row Background, based on a property value using data bindings