Why is DataAnnotations <Display(Name:=“My Name”)> ignored when using a DataGrid with AutoGenerateColumns=“True”

自闭症网瘾萝莉.ら 提交于 2019-12-20 14:24:54

问题


I'm using the WPF DataGrid to bind to a collection of a custom class. Using AutoGenerateColumns="True" in the grid XAML, the grid is created and populated just fine, but the headings are the property names, as one would expect.

I tried specifying

<Display(Name:="My Name")> 

from the System.ComponentModel.DataAnnotations namespace and that has no effect. I also tried

<DisplayName("My Name")> 

from the System.ComponentModel name space but still the headings are not affected.

Is there no way to specify column headings with the AutoGenerateColumns option?


回答1:


Using @Marc's suggestion was the beginning of the solution, but taken on it's own, the AutoGenerated columns still have the property names as headings.

To get the DisplayName, you need to add a routine (in the code behind) to handle the GridAutoGeneratingColumn event:

Private Sub OnGeneratingColumn(sender As Object, e As System.Windows.Controls.DataGridAutoGeneratingColumnEventArgs) Handles Grid.AutoGeneratingColumn
    Dim pd As System.ComponentModel.PropertyDescriptor = e.PropertyDescriptor
    e.Column.Header = pd.DisplayName
End Sub

An additional and better solution is to use the ComponentModel.DataAnnotations namespace and specify ShortName:

Public Class modelQ016
    <Display(shortname:="DB Name")>
    Public Property DBNAME As String
    ...

OnGeneratingColumn becomes:

        Dim pd As System.ComponentModel.PropertyDescriptor = e.PropertyDescriptor
        Dim DisplayAttrib As System.ComponentModel.DataAnnotations.DisplayAttribute =
            pd.Attributes(GetType(ComponentModel.DataAnnotations.DisplayAttribute))
        If Not DisplayAttrib Is Nothing Then
            e.Column.Header = DisplayAttrib.ShortName
        End If

Note that the order of attributes in the attribute array changes, so you must use the GetType(...) instead of a numeric parameter... Such fun!




回答2:


You might try the older System.ComponentModel.DisplayNameAttribute. In C# parlance, [DisplayName("My Name")]. In particular, this works with PropertyDescriptor, which underpins a lot of data-binding.




回答3:


By using @GilShalit post upon, this is what is needed to add in case that you are using Resources (as you probably should for multi culture language support) in C# in this time

Your property Declaration with Annotation

[Display(ResourceType = typeof (YourStronglyTypedMessagesResource), Name = "YourGridColumnName")]
public string Column1 { get; set; }

Event handler

private void DataGrid_OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    var pd = (PropertyDescriptor)e.PropertyDescriptor;
    var atb = (DisplayAttribute)pd.Attributes[typeof(DisplayAttribute)];
    if (atb != null)
    {
        e.Column.Header = atb.GetName();
    }
}


来源:https://stackoverflow.com/questions/5230402/why-is-dataannotations-displayname-my-name-ignored-when-using-a-datagrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!