枚举的组合框转换器
我有一个枚举,其中包含
HomeRun、StolenBase、FirstBase 等值。
我想在组合框中显示这些值,并在大写字母之前插入一个空格,因此它将显示为“Home Run”、“Stolen Base”, 我已经有了可以为我进行格式化的
代码,并且我已将该代码添加到 IValueConverter 实现的“Convert”方法中。
我的问题是,我需要在哪里使用这个转换器(在 xaml 中),这样不仅下拉列表,而且显示的值也将具有这种格式?我还需要实施 ConvertBack 吗?
我很清楚为枚举设置“描述”并使用流行的 EnumToDescriptionConverter,但我宁愿远离它。
I have an enumeration that has values like
HomeRun, StolenBase, FirstBase, etc.
I want to display these values in a combobox, with a space inserted before the capital letters, so it will display as 'Home Run', 'Stolen Base', etc.
I already have code that can do the formatting for me, and I have added that code to the 'Convert' method of an IValueConverter implementation.
My question is, where do I need to use this converter (in the xaml) such that not only the dropdown, but also the displayed value, will have this formatting? Do I need to implement ConvertBack as well?
I am well aware of setting 'descriptions' for the enumeration and using the popular EnumToDescriptionConverter, but I'd rather stay away from that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不确定是否有更好的方法,但您可以使用 ItemTemplate 实现您想要的效果,
这将在您的 ComboBox 中显示转换后的值。
ComboBox 的 SelectedValue 仍将是 Enum 值。您不需要实施 ConvertBack。
I'm not sure if there is a better way, but you can achieve what you want using an ItemTemplate
This will display the converted value in your ComboBox.
The SelectedValue of the ComboBox will still be the Enum value. You won't need to implement ConvertBack.
[更新] 我的答案的关键点是枚举值完全转换。我认为这种方式比隐藏每个枚举值更容易。[/updated]
在 ComboBox 的 Binding ItemsSource(
ItemsSource="{Binding Source={x:Null}, Converter={StaticResource converter}}"
) 处,请检查以下代码。不,你不需要。,因为在运行时你无法修改枚举,即使可以修改枚举,你也无法更改 VIEW 中 ComboBox 的 ItemsSource,这意味着 Binding Mode 是 OneWay。
XAML
代码
[updated] The key point of my answer is that the enum values are converted totally. I think this way is eaier than the coverting each enum value.[/updated]
At Binding ItemsSource of ComboBox(
ItemsSource="{Binding Source={x:Null}, Converter={StaticResource converter}}"
), Please check the following code.No, you don't., because at runtime you cannot modify the enumeration, and even though it can do, you cannot change ItemsSource of ComboBox in VIEW, which means Binding Mode is OneWay.
XAML
Code
如果您希望将 ComboBox 的选定值转换回为枚举,那么您需要实现
ConvertBack
。我个人会选择您提到的描述属性模式,因为
但假设您想采用这种模式,您只需要正确编写转换器即可。我建议这样:
您还可以创建一个简单的类来保存显示值和枚举,然后适当地设置组合框上的
DisplayPath
属性编辑
我意识到这不起作用,因为
ConvertBack
正在尝试将字符串转换为枚举,但实际的绑定集是List
。我将把它留在这里,因为这是一个正确方向的开始。我相信您需要两个转换器
ConvertBack
方法。正如我所指出的,如果您不实现
ConvertBack
,那么您将无法将 SelectedValue 绑定回 ViewModel 上的枚举属性。If you want the selected value of the ComboBox to be converted back to an enum then you will need to implement
ConvertBack
.I'd personally go with the description attribute pattern that you mentioned, because
But assuming you want to go with this pattern, you just need to write your converter correctly. I'd suggest something like this:
You could also create a simple class to hold both the display value and the enum, and then set the
DisplayPath
property on the combo box appropriatelyEdit
I realise that this won't work because the
ConvertBack
is attempting to convert a string to an enum, but the actual binding set is aList<string>
. I'll leave it here because it is a start in the right direction.I believe you'd need two converters
ConvertBack
method.As I pointed out, if you don't implement
ConvertBack
then you won't be able to bind the SelectedValue back to your enum Property on your ViewModel.您将需要创建一个字典或其他一些查找结构,将枚举值映射到字符串表示形式。
You will need to make a dictionary or some other lookup structure that maps the Enum value to the string representation.
有一个提示可以作为开始:
http://geekswithblogs .net/jawad/archive/2005/06/24/EnumDropDown.aspx
我开发了自己的枚举绑定助手从这个想法开始。
There is an hint that you can use as a start :
http://geekswithblogs.net/jawad/archive/2005/06/24/EnumDropDown.aspx
I developped my own enum binding helpers starting with that idea.