你怎么当的AutoGenerateColumns =真正的重命名的DataGrid列? [英] How do you rename DataGrid columns when AutoGenerateColumns = True?

查看:1456
本文介绍了你怎么当的AutoGenerateColumns =真正的重命名的DataGrid列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的数据结构类:

I have a simple data structure class:

public class Client {
    public String name {set; get;}
    public String claim_number {set; get;}
}



对此我送入一个的DataGrid

this.data_grid_clients.ItemSource = this.clients;



我想更改列标题。即:claim_number以索赔号码。我知道这是可以做到,当你手工做类似创建列:

I would like to change the column headings. Ie: claim_number to "Claim Number". I know this can be done when you manually create the columns by doing something like:

this.data_grid_clients.Columns[0].Header = "Claim Number"

但是,属性为空生成自动的列时。有没有办法重命名列,或者我需要手动生成列?

However, the Columns property is empty when auto-generating the columns. Is there a way to rename the columns, or do I have to manually generate the columns?

推荐答案

您可以使用的 DisplayNameAttribute ,然后更新您的部分代码来实现你想要的。

You can use DisplayNameAttribute and update some part of your code to achieve what you want.

你必须做的第一件事是,加入 [DisplayName的()] 在客户端类的属性。

The first thing you have to do is, adding a [DisplayName("")] to properties in the Client class.

public class Client {
    [DisplayName("Column Name 1")]
    public String name {set; get;}

    [DisplayName("Clain Number")]
    public String claim_number {set; get;}
}

在XAML代码,一个事件处理程序添加到AutoGenerationColumn事件更新。

The update you xaml code, add an event handler to AutoGenerationColumn event.

<dg:DataGrid AutoGenerateColumns="True" AutoGeneratingColumn="OnAutoGeneratingColumn">
</dg:DataGrid>



最后,添加一个方法来隐藏代码。

Finally, add a method to the code-behind.

private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    var displayName = GetPropertyDisplayName(e.PropertyDescriptor);

    if (!string.IsNullOrEmpty(displayName))
    {
        e.Column.Header = displayName;
    }

}

public static string GetPropertyDisplayName(object descriptor)
{
    var pd = descriptor as PropertyDescriptor;

    if (pd != null)
    {
        // Check for DisplayName attribute and set the column header accordingly
        var displayName = pd.Attributes[typeof(DisplayNameAttribute)] as DisplayNameAttribute;

        if (displayName != null && displayName != DisplayNameAttribute.Default)
        {
            return displayName.DisplayName;
        }

    }
    else
    {
        var pi = descriptor as PropertyInfo;

        if (pi != null)
        {
            // Check for DisplayName attribute and set the column header accordingly
            Object[] attributes = pi.GetCustomAttributes(typeof(DisplayNameAttribute), true);
            for (int i = 0; i < attributes.Length; ++i)
            {
                var displayName = attributes[i] as DisplayNameAttribute;
                if (displayName != null && displayName != DisplayNameAttribute.Default)
                {
                    return displayName.DisplayName;
                }
            }
        }
    }

    return null;
}

这篇关于你怎么当的AutoGenerateColumns =真正的重命名的DataGrid列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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