WPF 组合框绑定到枚举
我有一个组合框,它使用 ObjectDataProvider 将 ItemsSource 绑定到枚举,并且其 SelectedItem 属性绑定到业务对象的属性。 由于某种原因,它首先绑定 SelectedItem,然后绑定 ItemsSource,因此覆盖了我对 Businessobject 属性的默认设置。有什么想法以及可能的修复方法吗? 提前致谢。
XAML:
<CollectionViewSource x:Key="Units">
<CollectionViewSource.Source>
<ObjectDataProvider MethodName="GetNames" ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="BO:Unit"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</CollectionViewSource.Source>
</CollectionViewSource>
<ComboBox Grid.Column="1" HorizontalAlignment="Right" Width="80"
ItemsSource="{Binding Source={StaticResource Units}}"
SelectedItem="{Binding Path=Unit}"/>
I have a Combo Box that has ItemsSource Bound to an Enumeration using ObjectDataProvider, and its SelectedItem Property is bound to a property of a businessobject.
For some reason it's binding SelectedItem first and ItemsSource second, therefore overwriting my default on the businessobject property. Any ideas why and possibly a fix?
Thanks in Advance.
XAML:
<CollectionViewSource x:Key="Units">
<CollectionViewSource.Source>
<ObjectDataProvider MethodName="GetNames" ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="BO:Unit"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</CollectionViewSource.Source>
</CollectionViewSource>
<ComboBox Grid.Column="1" HorizontalAlignment="Right" Width="80"
ItemsSource="{Binding Source={StaticResource Units}}"
SelectedItem="{Binding Path=Unit}"/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我尝试了您的代码,它工作正常,所以我认为绑定的顺序不是您的问题。我注意到的一件事是,您使用
GetNames
作为ObjectDataProvider
的 MethodName,因此ComboBox
ItemsSource 将是字符串集合,而不是枚举Unit
。如果这是您的意图,那么属性Unit
的类型应为string
Example
如果您将
GetNames
更改为GetValues
它将适用于枚举类型Unit
示例的属性
I tried your code and it's working fine so I don't think that order of the Bindings is your problem. One thing I noticed is that you're using
GetNames
as the MethodName for theObjectDataProvider
so theComboBox
ItemsSource will be a Collection of strings and not of the enumUnit
. If this is your intention then the PropertyUnit
should be of typestring
Example
If you change
GetNames
toGetValues
it'll work for a Property of enum typeUnit
Example