Display “Display name” instead of field name in WP

2020-07-19 21:14发布

This is my Class definition.

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

This is my XAML code

<DataGrid Name="DataGrid" />

And this is data binding

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

This is the result:

DataGrid Result

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

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

1条回答
够拽才男人
2楼-- · 2020-07-19 21:54

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}"/>
查看更多
登录 后发表回答