gpt4 book ai didi

wpf - 将转换后的 Enum 绑定(bind)到 ComboBox

转载 作者:行者123 更新时间:2023-12-04 15:26:54 25 4
gpt4 key购买 nike

我试图将以下 Enum 绑定(bind)到 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>

我的问题是所选项目的绑定(bind)。它失败了,但我不知道为什么。

最佳答案

SelectedItem 的绑定(bind)是错误的,因为您将 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

关于wpf - 将转换后的 Enum 绑定(bind)到 ComboBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5576407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com