MVVM 绑定枚举值:大量代理属性
我正在编写一个与我的 WPF 自定义控件配合使用的 ViewModel 库。 我的问题是我的 DomainModel 有大量数据类型: Cd、笔、小工具、书籍等。 所有这些数据类型都用一个枚举来枚举(我有大约一百个数据类型),每个数据类型对应一个数据库表。
所以我们的想法是拥有一个 ViewModel 库,它为每种数据类型公开一个属性,这样我的 UI 控件就可以直接绑定我的 viewModel 的属性。每个属性的 viewModel 返回一个 ObservableCollection。 例如,如果我想让我的组合框填充“小工具”数据,在我的 XAML 中我将有类似 : 的内容
<my:XCombo ItemsSource="{Binding Gadgets}" .... />
,在我的 ViewModel 中我将有 :
public ObservableCollection<Gadgets> Gadgets
{
get
{
//get gadgets data from my domain model
return _model.GetData(DataEnum.Gadgets);
}
}
现在,为了做到这一点,我需要在我的 ViewModel 中,每个枚举值都有一个属性,但我想避免放置 100 个属性访问器。我很懒,这很容易出错。 我知道,在 c#4 中我们有动态属性,所以通过这种方式我可以避免编写 100 个属性访问器,但我必须使用没有动态属性的 .net 3.5,我不能使用 .net 4 ;(
有没有人已经遇到这个问题或有任何建议吗?
非常感谢。
I'm writing a ViewModel library which works with my WPF Custom Controls.
My problem is that my DomainModel has a large amount of Data Types:
Cd,Pens,Gadgets,Books,ecc.
All these Data Types are enumerated with an enum (I have more or less 1 hundred DataTypes), and each data Type corresponds to a DB table.
So the idea is to have a ViewModel library which exposes one property for each data type, thus my UI controls can directly bind the properties of my viewModel. The viewModel for each property return an ObservableCollection.
For instance, if I'd like to have my combo box populated with the "Gadgets" data, in my XAML I 'll have something like :
<my:XCombo ItemsSource="{Binding Gadgets}" .... />
and in my ViewModel I'll have :
public ObservableCollection<Gadgets> Gadgets
{
get
{
//get gadgets data from my domain model
return _model.GetData(DataEnum.Gadgets);
}
}
Now, in order to do that, I need in my ViewModel one property for each enumeration value, but I'd like to avoid to put 1 hundred property accessors. I'm lazy and this can be very error prone.
I know, in c#4 we have dynamic properties, so in this way I can avoid to write 100 property accessors, but I MUST use .net 3.5 which has no dynamic properties, i cannot use .net 4 ;(
Is there anyone who has already had this problem or any suggestion?
Thanks a lot in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试使用返回相应数据的索引器属性
然后使用该属性绑定它:
You could try to use an indexer property which returns the respective data
Then bind it using that: