开发者

DisplayNameAttribute for anonymous class

开发者 https://www.devze.com 2023-03-10 13:14 出处:网络
If I had a non-anonymous class like this, I know I can use DisplayNameAttribute like this. class Record{

If I had a non-anonymous class like this, I know I can use DisplayNameAttribute like this.

class Record{

    [DisplayName("The Foo")]
    public string Foo {get; set;}

    [DisplayName("The Bar")]
    public string Bar {get; set;}

}

but I have

var records = (from item in someCollection
               select{
                   Foo = item.SomeField,
                   Bar = item.SomeOtherField,
               }).ToList();

and I use records for DataSource for a DataGrid. The column head开发者_StackOverflow中文版ers show up as Foo and Bar but they have to be The Foo and The Bar. I cannot create a concrete class for a few different internal reasons and it will have to be an anonymous class. Given this, is there anyway I can set DisplayNameAttrubute for members of this anonymous class?

I tried

[DisplayName("The Foo")] Foo = item.SomeField

but it won't compile.

Thanks.


How about the following solution:

dataGrid.SetValue(
    DataGridUtilities.ColumnHeadersProperty,
    new Dictionary<string, string> {
        { "Foo", "The Foo" },
        { "Bar", "The Bar" },
    });

dataGrid.ItemsSource = (from item in someCollection
           select{
               Foo = item.SomeField,
               Bar = item.SomeOtherField,
           }).ToList();

Then you have the following Attached Property code:

public static class DataGridUtilities
{
    public static IDictionary<string,string> GetColumnHeaders(
        DependencyObject obj)
    {
        return (IDictionary<string,string>)obj.GetValue(ColumnHeadersProperty);
    }

    public static void SetColumnHeaders(DependencyObject obj,
        IDictionary<string, string> value)
    {
        obj.SetValue(ColumnHeadersProperty, value);
    }

    public static readonly DependencyProperty ColumnHeadersProperty =
        DependencyProperty.RegisterAttached(
            "ColumnHeaders",
            typeof(IDictionary<string, string>),
            typeof(DataGrid),
            new UIPropertyMetadata(null, ColumnHeadersPropertyChanged));

    static void ColumnHeadersPropertyChanged(DependencyObject sender,
        DependencyPropertyChangedEventArgs e)
    {
        var dataGrid = sender as DataGrid;
        if (dataGrid != null && e.NewValue != null)
        {
            dataGrid.AutoGeneratingColumn += AddColumnHeaders;
        }
    }

    static void AddColumnHeaders(object sender,
        DataGridAutoGeneratingColumnEventArgs e)
    {
        var headers = GetColumnHeaders(sender as DataGrid);
        if (headers != null && headers.ContainsKey(e.PropertyName))
        {
            e.Column.Header = headers[e.PropertyName];
        }
    }
}


As far as I know, you cannot apply an attribute to an anonymous type. The compiler simply doesn't support it. You could go really off the wagon and use something like Mono.Cecil as a post-build step to put the attribute there, but that's hardly something you want to consider. Why does it have to be anonymous?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号