样式化数据网格

Posted

技术标签:

【中文标题】样式化数据网格【英文标题】:Styling a Datagrid 【发布时间】:2014-06-11 03:55:12 【问题描述】:

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

我遇到的问题是,它将取决于 column[0] 中设置的值。

说 column[0] 的值是 1,2,3。我想根据这个值来设置数据网格的行。

例子

    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" />

一如既往地感谢您的帮助

【问题讨论】:

***.com/questions/18053281/… 【参考方案1】:

找到了答案,抱歉,在发布问题之前必须仔细查看。

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

【讨论】:

请关闭问题。此处不重复particulary welcome。【参考方案2】:

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

基本上:您需要一个 Valueconverter 来同时获取行/单元格的值。 在此处的示例中,我使用了 Multivalueconverter,因为我也需要有关 Colzumn 的信息 - 根据您的场景,普通的 Value-Converter 就足够了。

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#代码

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

【讨论】:

感谢 Christian,这对我来说是另一种选择,我找到了我正在寻找的答案(见上文)。感谢您花时间回答我的问题。

以上是关于样式化数据网格的主要内容,如果未能解决你的问题,请参考以下文章

如何在 WPF 中更改数据网格标题样式或元素样式前景

数据网格样式windows mobile c#

WPF 数据网格样式

如何结合两个条件来覆盖数据网格的选定行样式?

Android Compose 新闻App下拉刷新复杂数据网格布局文字样式

Android Compose 新闻App下拉刷新复杂数据网格布局文字样式