use*_*101 6 vb.net wpf binding ivalueconverter
我试图将以下枚举绑定到ComboBox
Public Enum PossibleActions
ActionRead
ActionWrite
ActionVerify
End Enum
Run Code Online (Sandbox Code Playgroud)
我不能改变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
Run Code Online (Sandbox Code Playgroud)
我的XAML看起来像
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:StepEditor"
Run Code Online (Sandbox Code Playgroud)
[...]
<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>
Run Code Online (Sandbox Code Playgroud)
[...]
要么:
<ComboBox ItemsSource="{Binding Source={StaticResource possibleActionsEnum}, Converter={StaticResource possibleActionsConverter}}"
SelectedItem="{Binding SelectedAction}"></ComboBox>
Run Code Online (Sandbox Code Playgroud)
要么:
<ComboBox ItemsSource="{Binding Source={StaticResource possibleActionsEnum}, Converter={StaticResource possibleActionsConverter}}"
SelectedItem="{Binding SelectedAction, Converter={StaticResource possibleActionsConverter}}"></ComboBox>
Run Code Online (Sandbox Code Playgroud)
我的问题是所选项目的绑定.它失败了,但我无法弄清楚原因.
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1669 次 |
| 最近记录: |