我正在根据我的模型的定义自动生成一堆列。我想对它们的显示顺序有更多的控制。从引发的事件中这样做是否合适,如下所示?
private void OnAutoGeneratedColumns(Object sender, EventArgs eventArgs) { ... }
或者,是否有一种方法可以对定义类的字段进行属性化,以便我可以控制它们显示的顺序?我将数据网格绑定到视图模型中的列表集合视图,在该视图中执行对要显示的行和要隐藏的行的筛选。我应该用这个来代替吗?
public ListCollectionView AllThingiesView { ... }
或者,也许这是一种不合适的方法来自动生成列呢?我正在添加我自己的一些,在模型中实际上没有相应的属性。
编辑
根据这些评论,我认识到这个问题可能会得到澄清。我得到了部分答案--使用DisplayOrder。这就回答了如何做它的问题,。但是,主要的问题是在哪里使用(或者when)来完成它。我已经提出了几个建议,关于该属性可能设置的位置,可能还有其他的(更好的)。
此外,似乎还不清楚什么是属性。我想知道是否可以使用属性控制/指定列的索引(我指的是属性上的属性,而不是属性本身)。例如,下面的伪代码显示了可以是一种可能的方法。
public class GridableThing
{
[Index(3)]
public String FirstProperty{ get; set; })
[Index(1)]
public String SecondProperty{ get; set; })
[Index(2)]
public String ThirdProperty{ get; set; })
}
在上面的例子中,如果没有以其他方式设置,列将以第一、第二和第三的顺序出现。通过使用网格的属性DisplayIndex,我可以使用属性的属性(属性)来排序它们。这种方法有可能吗?
发布于 2015-02-10 06:53:21
在我看来,AutoGeneratingColumns
事件处理程序并不适合插入管理列顺序的逻辑。当完成所有列的自动生成时,就会发生此事件,因此,当创建所有列时,您将插入一个混合它们的逻辑。可能最好是直接按照正确的顺序创建它们,而不是在第二时间重新排序。
此外,正如您所建议的,使用属性设置列顺序(类似于订单 )是很好的。在我看来,这样做意味着要描述一个类,所以我对您的问题的回答是:将您的逻辑放入CustomTypeDescriptor
中。好吧,也许你需要写更多的代码,你可以说这是解决问题的一条很长的路。也许我错了,但我更喜欢这样的方法:“这是我类的列(即属性)的顺序(或者是我的模型)”。
当然,为了将类链接到CustomTypeDescriptor,需要使用TypeDescriptionProvider
属性:
[TypeDescriptionProvider(typeof(CustomTypeDescriptionProvider))]
public class Party : INotifyPropertyChanged
{
/* ... */
}
并扩展TypeDescriptionProvider
类:
public class CustomTypeDescriptionProvider : TypeDescriptionProvider
{
private static TypeDescriptionProvider defaultTypeProvider =
TypeDescriptor.GetProvider(typeof(Party));
public CustomTypeDescriptionProvider()
: base(defaultTypeProvider)
{
}
public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
{
ICustomTypeDescriptor defaultDescriptor =
base.GetTypeDescriptor(objectType, instance);
return new CustomTypeDescriptor(defaultDescriptor, instance);
}
public ICustomTypeDescriptor GetBaseTypeDescriptor(Type objectType, object instance)
{
return base.GetTypeDescriptor(objectType, instance);
}
}
最后一步是创建您的CustomTypeDescriptor
class CustomTypeDescriptor : System.ComponentModel.CustomTypeDescriptor
{
private List<PropertyDescriptor> propertyDescriptors = new List<PropertyDescriptor>();
private PropertyDescriptorCollection propertyDescriptorCollection;
public CustomTypeDescriptor(ICustomTypeDescriptor parent, object instance)
: base(parent)
{
CustomTypeDescriptionProvider customTypeDescriptionProvider = new WpfApplication1.CustomTypeDescriptionProvider();
ICustomTypeDescriptor typeDescriptor = customTypeDescriptionProvider.GetBaseTypeDescriptor(typeof(Party), null);
PropertyDescriptorCollection tempPropertyDescriptorCollection = typeDescriptor.GetProperties();
propertyDescriptors.Add(tempPropertyDescriptorCollection[2]);
propertyDescriptors.Add(tempPropertyDescriptorCollection[1]);
propertyDescriptors.Add(tempPropertyDescriptorCollection[0]);
/* put here your ordering logic */
propertyDescriptorCollection = new PropertyDescriptorCollection(propertyDescriptors.ToArray());
}
public override PropertyDescriptorCollection GetProperties()
{
return propertyDescriptorCollection;
}
public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
return propertyDescriptorCollection;
}
}
这里,我使用默认的TypeDescriptor
来检索PropertyDescriptors of Party
对象。然后,按照我喜欢的方式对它们进行排序(最终,我可以读取属性以确定正确的顺序)。
很高兴听到一些对我的想法的反馈。
https://stackoverflow.com/questions/28411066
复制