Why is DataAnnotations ignored when using a DataGrid with AutoGenerateColumns=“True”

后端 未结 3 1645
迷失自我
迷失自我 2021-02-06 12:27

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

3条回答
  •  心在旅途
    2021-02-06 13:00

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

提交回复
热议问题