将转换后的枚举绑定到组合框

发布于 2024-10-30 21:43:23 字数 2567 浏览 5 评论 0原文

我尝试将以下枚举绑定到组合框,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光礼记 2024-11-06 21:43:23

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

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:

  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
紫﹏色ふ单纯 2024-11-06 21:43:23

iMHO 这里最好的解决方案是使用数据模板,因此您不必像上一个答案那样实现 2 个转换器(一个转换单个枚举,另一个转换数组)。使用数据模板,转换器可以单独应用于下拉列表中的每个项目,因此不再需要转换数组的转换器。

 <ComboBox.ItemTemplate>
   <DataTemplate>
     <TextBlock Text="{Binding Converter={StaticResource possibleActionConverter}}" />
   </DataTemplate>
 </ComboBox.ItemTemplate>

使用 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.

 <ComboBox.ItemTemplate>
   <DataTemplate>
     <TextBlock Text="{Binding Converter={StaticResource possibleActionConverter}}" />
   </DataTemplate>
 </ComboBox.ItemTemplate>

with possibleActionConverter converting a single enum to string.

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文