显示“显示名称”而不是WPF DataGrid中的字段名称 [英] Display "Display name" instead of field name in WPF DataGrid

查看:379
本文介绍了显示“显示名称”而不是WPF DataGrid中的字段名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的班级定义。

    public class Customer
    {
        [Display(Name="Customer ID")]
        public int ID { get; set; }
        [Display(Name="Customer Name")]
        public string CusName { get; set; }
    }

这是我的XAML代码

< DataGrid Name = DataGrid />

这是数据绑定

    public Test()
    {
        InitializeComponent();


        List<Customer> cus = new List<Customer>();
        cus.Add(new Customer() { ID = 1, CusName = "Jackson" });
        cus.Add(new Customer() { ID = 2, CusName = "Micheal" });
        cus.Add(new Customer() { ID = 3, CusName = "Jackson" });
        DataGrid.ItemsSource = cus;
    }

结果如下:

DataGrid标头列显示ID,CusName,它们是客户的字段名称。

The DataGrid header columns diplay the ID,CusName, they are Customer's field name.

如何使DataGrid标题列显示客户ID,而不是ID,CusNumber是客户名?它们位于[Display]属性中

How to make DataGrid header column display Customer ID, Customer Name instead of ID, CusNumber ? They are in [Display] attribute

推荐答案

如您所见,DataGrid并不在乎是否装饰了属性。您可以禁用列的自动生成并手动定义它们,或者利用AutoGeneratingColumn事件

As you can see DataGrid doesn' really care if you properties are decorated. You can either disable the autogeneration of columns and define them manually, or leverage the AutoGeneratingColumn event

<DataGrid x:Name="grid" AutoGeneratingColumn="grid_AutoGeneratingColumn"/>


private void grid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
        switch (e.PropertyName)
        {
            case "ID":
                e.Column.Header = "Customer ID";
                break;

            case "CusName":
                e.Column.Header = "Customer Name";
                break;

            default:
                break;
        }
    }

您还可以通过定义附加行为来自动执行此解决方案:

You can also automate this solution by defining an attached behaviour:

public static class CustomColumnHeadersProperty
{
    public static DependencyProperty ItemTypeProperty = DependencyProperty.RegisterAttached(
        "ItemType",
        typeof(Type),
        typeof(CustomColumnHeadersProperty),
        new PropertyMetadata(OnItemTypeChanged));

    public static void SetItemType(DependencyObject obj, Type value)
    {
        obj.SetValue(ItemTypeProperty, value);
    }

    public static Type GetItemType(DependencyObject obj)
    {
        return (Type)obj.GetValue(ItemTypeProperty);
    }

    private static void OnItemTypeChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
    {
        var dataGrid = sender as DataGrid;

        if (args.NewValue != null)
            dataGrid.AutoGeneratingColumn += dataGrid_AutoGeneratingColumn;
        else
            dataGrid.AutoGeneratingColumn -= dataGrid_AutoGeneratingColumn;
    }

    static void dataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
        var type = GetItemType(sender as DataGrid);

        var displayAttribute = type.GetProperty(e.PropertyName).GetCustomAttributes(typeof(DisplayAttribute), false).FirstOrDefault() as DisplayAttribute;
        if (displayAttribute != null)
            e.Column.Header = displayAttribute.Name;
    }
}


<DataGrid x:Name="grid" local:CustomColumnHeadersProperty.ItemType="{x:Type local:MyClass}"/>

这篇关于显示“显示名称”而不是WPF DataGrid中的字段名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆