WPF DataGrid - 我可以使用属性来装饰我的POCO以具有自定义列名吗? [英] WPF DataGrid - Can I decorate my POCOs with attributes to have custom column names?
问题描述
public enum Sharing
{
等于
SurfaceBased,
}
public class Data
{
public bool Active {get;组; }
public string Name {get;组; }
public int Floor {get;组; }
public Sharing Sharing {get;组; }
}
public ObservableCollection< Data> _col = new ObservableCollection< Data>()
{
new Data(){Active = true,Name =KRL,Floor = 0},
new Data(){Name = DAT,Floor = 1},
new Data(){Name =TRE,Floor = 1},
new Data(){Name =DUO,Floor = 2},
};
public MainWindow()
{
InitializeComponent();
grid.AutoGenerateColumns = true;
grid.DataContext = _col;
grid.ItemsSource = _col;
}
我想知道是否可以在枚举和POCO类上使用一些属性以便DataGrid在标题和ComboCoxes上显示它们(而不是变量名)。
这样的一个例子:
public enum Sharing
{
[Name(This is a test)]
等于
[Name(This是一个测试2)]
SurfaceBased,
}
?
可以。这是为标题执行的方法:
您可以向属性添加属性,例如描述
属性。
public class MyPOCO
{
[说明(您必须支付的金额)]
public float Amount {get;组; }
}
然后,在从DataGrid派生的类中,您可以执行以下操作:
protected override void OnAutoGeneratingColumn(DataGridAutoGeneratingColumnEventArgs e)
{
try
{
base。 OnAutoGeneratingColumn(e);
var propDescr = e.PropertyDescriptor as System.ComponentModel.PropertyDescriptor;
e.Column.Header = propDescr.Description;
}
catch(Exception ex)
{
Utils.ReportException(ex);
}
}
要向枚举成员添加自定义名称,您需要自定义列。您可以在这里看到一个简单的例子: http://stackoverflow.com/a/17510660/964053 。
I have a DataGrid in WPF and fill it with data like this:
public enum Sharing
{
Equal,
SurfaceBased,
}
public class Data
{
public bool Active { get; set; }
public string Name { get; set; }
public int Floor { get; set; }
public Sharing Sharing { get; set; }
}
public ObservableCollection<Data> _col = new ObservableCollection<Data>()
{
new Data(){Active = true, Name = "KRL", Floor = 0 },
new Data(){Name = "DAT", Floor = 1},
new Data(){Name = "TRE", Floor = 1},
new Data(){Name = "DUO", Floor = 2},
};
public MainWindow()
{
InitializeComponent();
grid.AutoGenerateColumns = true;
grid.DataContext = _col;
grid.ItemsSource = _col;
}
I was wondering if I could use some attributes on the enumerations and the POCO class so that the DataGrid displays them (instead of the variable names) on the headers and ComboCoxes.
Something like this:
public enum Sharing
{
[Name("This is a test")]
Equal,
[Name("This is a test 2")]
SurfaceBased,
}
Is this possible?
OK. Here is the way to do it for the Headers:
You add attributes, like Description
attributes to your Properties.
public class MyPOCO
{
[Description("The amount you must pay")]
public float Amount { get; set; }
}
Then, in a class derived from DataGrid you do this:
protected override void OnAutoGeneratingColumn(DataGridAutoGeneratingColumnEventArgs e)
{
try
{
base.OnAutoGeneratingColumn(e);
var propDescr = e.PropertyDescriptor as System.ComponentModel.PropertyDescriptor;
e.Column.Header = propDescr.Description;
}
catch (Exception ex)
{
Utils.ReportException(ex);
}
}
For adding custom names to the members of the enumerations, you need to make a custom column. You can see a simple example here : http://stackoverflow.com/a/17510660/964053.
这篇关于WPF DataGrid - 我可以使用属性来装饰我的POCO以具有自定义列名吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!