问题描述
我有一组对象,我想将它们绑定到 RadGridView(来自工具包 Telerik).对象的类看起来像这样(需要理解的最少代码),其中我有 1 个属性和 1 个值数组:
I have a collection of objects which I want to bind to a RadGridView (from the toolkit telerik). The class of the objects is looking like that (minimum code needed to understand) where I have 1 property and 1 array of values which :
public class AttributeEntry : INotifyPropertyChanged
{
public string Code { get; set; }
private string[] _values;
public string[] Values
{
get { return _values; }
set { _values = value; }
}
public string this[int index]
{
get { return _values[index]; }
set
{
_values[index] = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(Binding.IndexerName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
是否有人知道一种(简单的)方法,使用模式 MVVM 来定义一个 RadGridView,其中定义了一些列(在我的情况下对应于代码"属性)和一些列自动生成",每列对应于元素集合的(在我的例子中是数组值"的元素.如果我的集合中有 7 个值,我应该有 7 个自动生成"列)?
Is someone know a (simple) way, using the patern MVVM, to have a RadGridView with some columns defined (in my case corresponding to the 'Code' property) and some columns "autogenerated" with each columns corresponding to the elements of a collection (in my case the elements of the array 'Values". If I have 7 values in my collection, I should have 7 "autogenerated" columns)?
推荐答案
对于任何试图实现这一点的人,将您的类声明为 expandoobject 这是一个具有实现 INotifyPropertyChanged 的灵活类结构,您可以根据需要在代码中定义属性.
For anybody who is trying to implement this, declare your class as an expandoobject this a flexible class structure with implements INotifyPropertyChanged and you can define the properties as required in code.
在 Telerik raddatagrid 上将 itemsource 绑定到 expandoobjects 的集合,并将 AutoGenerateColumns 设置为 true.
On the telerik raddatagrid bind the itemsource to a collection of the expandoobjects and set AutoGenerateColumns to true.
如果您想根据 expandoobject 的内容定制列,然后使用 AutoGeneratingColumn 事件,该事件会在每个列生成时触发,如果您将列控件定义为数据模板,您可以在后面的代码中访问它们并分配它们单元格模板也是如此.
If you want to tailor the columns based upon the contents of the expandoobject then use the AutoGeneratingColumn event, this is fired for each column generate, if you define your column controls as datatemplates you can access them in the code behind and assign them too the cell template.
这篇关于将 MVVM 与(仅)从集合自动生成的一些列绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!