将组合框绑定到枚举...在 Silverlight 中!
因此,Web 和 StackOverflow 对于如何将组合框绑定到 WPF 中的枚举属性有很多很好的答案。但是 Silverlight 缺少使这成为可能的所有功能:(。例如:
- 您不能使用通用的
EnumDisplayer
-样式IValueConverter
接受类型参数,因为 Silverlight 不支持 < code>x:Type - 不能使用
ObjectDataProvider
,如 此方法,因为它在 Silverlight 中不存在。 - 您不能像注释中那样使用自定义标记扩展在 #2 的链接上,因为 Silverlight 中不存在标记扩展,因此
- 您无法使用泛型而不是对象的
Type
属性来执行 #1 的版本,因为不支持泛型。在 XAML 中(并且使它们工作的技巧全部依赖于标记扩展,在 Silverlight 中不受支持)
欺骗并绑定到我的 ViewModel 中的字符串
正如我所见,使这项工作有效的唯一方法是
- 属性 。 ,其 setter/getter 执行转换,使用视图中的代码隐藏将值加载到 ComboBox 中。
- 为我想要绑定的每个枚举创建一个自定义的
IValueConverter
。
是否有更通用的替代方案,即不涉及为我想要的每个枚举一遍又一遍地编写相同的代码?我想我可以使用接受枚举作为类型参数的泛型类来执行解决方案#2,然后为我想要的每个枚举创建新类,这就是
class MyEnumConverter : GenericEnumConverter<MyEnum> {}
你们的想法,伙计们?
So, the web, and StackOverflow, have plenty of nice answers for how to bind a combobox to an enum property in WPF. But Silverlight is missing all of the features that make this possible :(. For example:
- You can't use a generic
EnumDisplayer
-styleIValueConverter
that accepts a type parameter, since Silverlight doesn't supportx:Type
. - You can't use
ObjectDataProvider
, like in this approach, since it doesn't exist in Silverlight. - You can't use a custom markup extension like in the comments on the link from #2, since markup extensions don't exist in Silverlight.
- You can't do a version of #1 using generics instead of
Type
properties of the object, since generics aren't supported in XAML (and the hacks to make them work all depend on markup extensions, not supported in Silverlight).
Massive fail!
As I see it, the only way to make this work is to either
- Cheat and bind to a string property in my ViewModel, whose setter/getter does the conversion, loading values into the ComboBox using code-behind in the View.
- Make a custom
IValueConverter
for every enum I want to bind to.
Are there any alternatives that are more generic, i.e. don't involve writing the same code over and over for every enum I want? I suppose I could do solution #2 using a generic class accepting the enum as a type parameter, and then create new classes for every enum I want that are simply
class MyEnumConverter : GenericEnumConverter<MyEnum> {}
What are your thoughts, guys?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
啊,我说得太早了!有一个完美的解决方案,至少在 Silverlight 3 中。(可能只在 3 中,因为 此线程 表示与此内容相关的错误已在 Silverlight 3 中修复。)
基本上,您需要一个用于
ItemsSource
属性的转换器,但它可以完全通用,无需使用任何禁止的方法,如只要您向其传递类型为 MyEnum 的属性名称即可。并且将数据绑定到SelectedItem
是完全轻松的;无需转换器!好吧,至少只要您不希望通过例如DescriptionAttribute
为每个枚举值自定义字符串,嗯...可能需要另一个转换器;希望我能让它通用。更新:我制作了一个转换器并且它可以工作!遗憾的是,我现在必须绑定到
SelectedIndex
,但没关系。使用这些人:使用这种绑定 XAML:
以及这种资源声明 XAML:
任何评论都会受到赞赏,因为我在某种程度上是 XAML/Silverlight/WPF/等。新手。例如,
EnumToIntConverter.ConvertBack
会很慢吗?我应该考虑使用缓存吗?Agh, I spoke too soon! There is a perfectly good solution, at least in Silverlight 3. (It might only be in 3, since this thread indicates that a bug related to this stuff was fixed in Silverlight 3.)
Basically, you need a single converter for the
ItemsSource
property, but it can be entirely generic without using any of the prohibited methods, as long as you pass it the name of a property whose type isMyEnum
. And databinding toSelectedItem
is entirely painless; no converter needed! Well, at least it is as long as you don't want custom strings for each enum value via e.g. theDescriptionAttribute
, hmm... will probably need another converter for that one; hope I can make it generic.Update: I made a converter and it works! I have to bind to
SelectedIndex
now, sadly, but it's OK. Use these guys:With this sort of binding XAML:
And this sort of resource-declaration XAML:
Any comments would be appreciated, as I'm somewhat of a XAML/Silverlight/WPF/etc. newbie. For example, will the
EnumToIntConverter.ConvertBack
be slow, so that I should consider using a cache?还有另一种方法可以将 ComboBox 绑定到枚举,而不需要为所选项目使用自定义转换器。您可以在
http:// /charlass.wordpress.com/2009/07/29/binding-enums-to-a-combobbox-in-silverlight/
它不使用 DescriptionAttributes....但它非常适合我,所以我想这取决于它的使用场景
There is another way to bind ComboBox to enums without the need of a custom converter for the selected item. You can check it at
http://charlass.wordpress.com/2009/07/29/binding-enums-to-a-combobbox-in-silverlight/
It doesn't use the DescriptionAttributes.... but it works perfectly for me, so i guess it depends on the scenario it will be used
我发现枚举数据的简单封装更容易使用。
...
这将完全消除对转换器的需要...:)
I find that a simple encapsulation of enum data is way easier to use.
...
And this will completly remove the need of a converter... :)
这是 Windows 8.1/Windows Phone 通用应用程序的相同设置,主要变化是: -
看起来XAML 的顺序也很重要,我必须将 ItemsSource 放在 SelectedIndex 之前,否则它不会调用 ItemsSource 绑定
例如
下面的代码
Here is the same setup for a Windows 8.1/Windows Phone universal App, main changes are:-
It seems that the order of the XAML is important too, I had to put ItemsSource before SelectedIndex otherwise it didn't call the ItemsSource binding
e.g.
Code below