2011-04-07 131 views
6

我試着以下枚舉綁定到組合框綁定轉換枚舉到一個ComboBox

Public Enum PossibleActions 
    ActionRead 
    ActionWrite 
    ActionVerify 
End 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> 

Or:

<ComboBox ItemsSource="{Binding Source={StaticResource possibleActionsEnum}, Converter={StaticResource possibleActionsConverter}}" 
      SelectedItem="{Binding SelectedAction, Converter={StaticResource possibleActionsConverter}}"></ComboBox> 

我的問題是所選項目的綁定。它失敗了,但我不明白爲什麼。

回答

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

最好的解決辦法在這裏恕我直言,是使用數據模板,這樣你就不必實現2個轉換器,在前面的答案(一個將單枚舉,而另一個轉換數組)。使用數據完成時,可以將轉換器分別應用於每個下拉列表項,因此不再需要轉換數組的轉換器。

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

with possibleActionConverter將單個枚舉轉換爲字符串。

HTH