我尝试将以下Enum绑定到ComboBox
Public Enum PossibleActions
ActionRead
ActionWrite
ActionVerify
End Enum
我无法更改Enum本身,但我不想显示这些字符串。我的目的只是剪切前缀'Action'并在ComboBox中显示'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>
我的问题是所选项目的绑定。它失败了,但我无法弄清楚原因。
答案 0 :(得分:3)
SelectedItem的绑定是错误的,因为您将Enum转换为字符串,但SelectedItems是单个字符串。如果您想坚持这种架构,请编写一个转换器,将单个字符串转换回您的枚举。 现有转换器的Convert和ConvertBack方法接近解决方案。它们看起来像:
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 action As PossibleActions
action = CType(value, PossibleActions)
Return action.ToString.Substring(6)
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
答案 1 :(得分:1)
iMHO的最佳解决方案是使用数据模板,因此您不必像上一个答案那样实现2个转换器(一个转换单个枚举,另一个转换数组)。使用数据完成,转换器可以单独应用于下拉菜单的每个项目,因此不再需要转换数组的转换器。
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource possibleActionConverter}}" />
</DataTemplate>
</ComboBox.ItemTemplate>
with possibleActionConverter将单个枚举转换为字符串。
HTH