将转换后的枚举绑定到组合框
我尝试将以下枚举绑定到组合框,
Public Enum PossibleActions
ActionRead
ActionWrite
ActionVerify
End Enum
我无法更改枚举本身,但我不想显示这些字符串。我的目的只是删除前缀“Action”并在组合框中显示“Read”、“Write”和“Verify”。因此我写了一个 ValueConverter
Public Class PossibleActionsConverter
Implements IValueConverter
Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
Dim actions() As PossibleActions
Dim strings() As String
actions = CType(value, PossibleActions())
ReDim strings(actions.GetUpperBound(0))
For i = 0 To actions.GetUpperBound(0)
strings(i) = actions(i).ToString.Substring(6)
Next
Return strings
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
Dim s As String
s = CStr(value)
Return [Enum].Parse(GetType(PossibleActions), "Action" & s)
End Function
End Class
我的 XAML 看起来像
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:StepEditor"
[...]
<Window.Resources>
<ObjectDataProvider x:Key="possibleActionsEnum" MethodName="GetValues"
ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:TypeExtension Type="local:PossibleActions"></x:TypeExtension>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<local:PossibleActionsConverter x:Key="possibleActionsConverter"></local:PossibleActionsConverter>
</Window.Resources>
[...]
要么:
<ComboBox ItemsSource="{Binding Source={StaticResource possibleActionsEnum}, Converter={StaticResource possibleActionsConverter}}"
SelectedItem="{Binding SelectedAction}"></ComboBox>
要么:
<ComboBox ItemsSource="{Binding Source={StaticResource possibleActionsEnum}, Converter={StaticResource possibleActionsConverter}}"
SelectedItem="{Binding SelectedAction, Converter={StaticResource possibleActionsConverter}}"></ComboBox>
我的问题是所选项目的绑定。它失败了,但我不明白为什么。
I tried to bind the following Enum to a ComboBox
Public Enum PossibleActions
ActionRead
ActionWrite
ActionVerify
End Enum
I can't change the Enum itself, but I do not want to display these strings. My intention is just to cut the prefix 'Action' and display 'Read', 'Write' and 'Verify' in the ComboBox. Therefore I wrote a ValueConverter
Public Class PossibleActionsConverter
Implements IValueConverter
Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
Dim actions() As PossibleActions
Dim strings() As String
actions = CType(value, PossibleActions())
ReDim strings(actions.GetUpperBound(0))
For i = 0 To actions.GetUpperBound(0)
strings(i) = actions(i).ToString.Substring(6)
Next
Return strings
End Function
Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
Dim s As String
s = CStr(value)
Return [Enum].Parse(GetType(PossibleActions), "Action" & s)
End Function
End Class
My XAML looks like
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:StepEditor"
[...]
<Window.Resources>
<ObjectDataProvider x:Key="possibleActionsEnum" MethodName="GetValues"
ObjectType="{x:Type sys:Enum}">
<ObjectDataProvider.MethodParameters>
<x:TypeExtension Type="local:PossibleActions"></x:TypeExtension>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<local:PossibleActionsConverter x:Key="possibleActionsConverter"></local:PossibleActionsConverter>
</Window.Resources>
[...]
Either:
<ComboBox ItemsSource="{Binding Source={StaticResource possibleActionsEnum}, Converter={StaticResource possibleActionsConverter}}"
SelectedItem="{Binding SelectedAction}"></ComboBox>
Or:
<ComboBox ItemsSource="{Binding Source={StaticResource possibleActionsEnum}, Converter={StaticResource possibleActionsConverter}}"
SelectedItem="{Binding SelectedAction, Converter={StaticResource possibleActionsConverter}}"></ComboBox>
My problem is the binding of the selected item. It fails, but I can't figure out why.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SelectedItem 的绑定是错误的,因为您将 Enum 转换为字符串,但 SelectedItems 是单个字符串。如果您想坚持这种架构,请编写一个转换器将单个字符串转换回您的枚举。
现有转换器的 Convert 和 ConvertBack 方法接近解决方案。它们可以看起来像:
The binding of SelectedItem is wrong, because you convert your Enum into Strings, but SelectedItems is a single string. If you want to stick on this architecture, write a converter that converts a single string back to your enum.
The Convert and ConvertBack-methods of your existing converter are close to the solution. They can look like:
iMHO 这里最好的解决方案是使用数据模板,因此您不必像上一个答案那样实现 2 个转换器(一个转换单个枚举,另一个转换数组)。使用数据模板,转换器可以单独应用于下拉列表中的每个项目,因此不再需要转换数组的转换器。
使用 possibleActionConverter 将单个枚举转换为字符串。
华泰
The best solution here iMHO is to use a data template, so you don't have to implement 2 converters as in the previous answer (one that converts a single enum, and another that converts an array). Using a datatemplete, the converter can be applied to each item of the drop down individually, so the converter that converts an array is no longer needed.
with possibleActionConverter converting a single enum to string.
HTH