Databinding an enum property to a ComboBox in WPF(将枚举属性数据绑定到 WPF 中的组合框)
问题描述
以如下代码为例:
我希望将属性 ExampleProperty 数据绑定到 ComboBox,以便它显示选项FooBar"和BarFoo"并在 TwoWay 模式下工作.理想情况下,我希望我的 ComboBox 定义看起来像这样:
I want a to databind the property ExampleProperty to a ComboBox, so that it shows the options "FooBar" and "BarFoo" and works in mode TwoWay. Optimally I want my ComboBox definition to look something like this:
目前我在我的窗口中安装了 ComboBox.SelectionChanged 和 ExampleClass.PropertyChanged 事件的处理程序,我在其中手动进行绑定.
Currently I have handlers for the ComboBox.SelectionChanged and ExampleClass.PropertyChanged events installed in my Window where I do the binding manually.
有更好的或某种规范的方法吗?您通常会使用转换器吗?如何使用正确的值填充 ComboBox?我现在什至不想开始使用 i18n.
Is there a better or some kind of canonical way? Would you usually use Converters and how would you populate the ComboBox with the right values? I don't even want to get started with i18n right now.
编辑
所以回答了一个问题:如何使用正确的值填充 ComboBox.
So one question was answered: How do I populate the ComboBox with the right values.
从静态 Enum.GetValues 方法中通过 ObjectDataProvider 将枚举值作为字符串列表检索:
Retrieve Enum values as a list of strings via an ObjectDataProvider from the static Enum.GetValues method:
这可以用作我的 ComboBox 的 ItemsSource:
This I can use as an ItemsSource for my ComboBox:
推荐答案
您可以创建自定义标记扩展.
You can create a custom markup extension.
使用示例:
在 XAML 的顶部:
At the top of your XAML:
然后……
以及实现...
这篇关于将枚举属性数据绑定到 WPF 中的组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!