2016-05-07 89 views
0

我試圖將Enum綁定到ComboBox。我看到很多人使用ObjectDataProvider,但我似乎無法訪問它。我也注意到,有些人在Window.Resources而不是Page.Resources內使用它,但我無法找到它在Page.Resources上的使用方式。我一直在尋找解決方案几個小時。如何使用ObjectDataProvider將枚舉綁定到XAML中的ComboBox

我到目前爲止有:

XAML

<Page 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="clr-namespace:Sports;assembly=Sports" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:ViewModel="using:Sports.ViewModel" 
xmlns:model="using:Sports.Model" 
xmlns:system="using:System" 


x:Class="Sports.MainPage" 
mc:Ignorable="d"> 

<Page.DataContext> 
    <ViewModel:CreateSubsVM/> 
</Page.DataContext> 
    <Page.Resources> 

    <ObjectDataProvider></ObjectDataProvider> 
    </Page.Resources> 
    </Grid> 
</Page> 

C#

public enum SubsAmount 
{ 
    [Display(Description = "One Year")] 
    Oneyear = 0, 
    [Display(Description = "Two Years")] 
    TwoYears = 1, 
    [Display(Description = "Three Years")] 
    ThreeYears = 2 
} 


public class ComboboxConverter: IValueConverter 
{ 

    public string GetEnumValues(Enum enumObj) 
    { 
     DisplayAttribute attribute = enumObj.GetType(). 
     GetRuntimeField(enumObj.ToString()). 
     GetCustomAttributes(typeof(SubsAmount), false). 
     SingleOrDefault() as DisplayAttribute; 
     return attribute == null ? enumObj.ToString() : attribute.Description; 
    } 


    public object Convert(object value, Type targetType, object parameter, string language) 
    { 
     return GetEnumValues((Enum) value); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, string language) 
    { 
     return Enum.ToObject(targetType, value); 
    } 
} 
+0

我對你是如何調用轉換器有點不清楚。你離開了多少代碼? –

+0

_「我似乎無法訪問它」_ - 這是什麼意思?您上面顯示的代碼無法成功使用ObjectDataProvider,因爲您已經使用空元素聲明瞭它。它只在您提供必要的值時纔有用。正確完成,它會正常工作。請提供一個很好的[mcve],它清楚地顯示了你所嘗試的內容,以及一個錯誤描述。 –

回答

0

這裏是一個頁面對象的例子(根據MSDN documentation有在頁面中使用ObjectDataProvider沒有任何限制):

更新#1

的XAML

<Page x:Class="PageBasedApp.MyPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:pageBasedApp="clr-namespace:PageBasedApp" 
    mc:Ignorable="d" 
    d:DesignHeight="300" d:DesignWidth="300" 
Title="MyPage"> 
<Page.Resources> 
    <ObjectDataProvider x:Key="Gestures" MethodName="GetValues" ObjectType="{x:Type ApplicationGesture}"> 
     <ObjectDataProvider.MethodParameters> 
      <x:Type TypeName="ApplicationGesture" /> 
     </ObjectDataProvider.MethodParameters> 
    </ObjectDataProvider> 
    <ObjectDataProvider x:Key="SubAmounts" MethodName="GetShortListOfApplicationGestures" ObjectType="{x:Type pageBasedApp:DisplayAttributeBasedObjectDataProvider}"> 
     <ObjectDataProvider.MethodParameters> 
      <x:Type TypeName="pageBasedApp:SubsAmount" /> 
     </ObjectDataProvider.MethodParameters> 
    </ObjectDataProvider> 
</Page.Resources> 

<Grid> 
    <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" > 
     <Label Content="All Gestures:"/> 
     <ComboBox ItemsSource="{Binding Source={StaticResource Gestures}}" Width="150"/> 
     <Label Content="Sub Amounts:"/> 
     <ComboBox ItemsSource="{Binding Source={StaticResource SubAmounts}}" Width="150"/> 
    </StackPanel> 
</Grid> 

這裏是一個自定義數據提供代碼

public class DisplayAttributeBasedObjectDataProvider : ObjectDataProvider 
{ 
    public object GetEnumValues(Enum enumObj) 
    { 
     var attribute = enumObj.GetType().GetRuntimeField(enumObj.ToString()). 
      GetCustomAttributes(typeof(DisplayAttribute), false). 
      SingleOrDefault() as DisplayAttribute; 
     return attribute == null ? enumObj.ToString() : attribute.Description; 
    } 

    public List<object> GetShortListOfApplicationGestures(Type type) 
    { 
     var shortListOfApplicationGestures = Enum.GetValues(type).OfType<Enum>().Select(GetEnumValues).ToList(); 
     return 
      shortListOfApplicationGestures; 
    } 
} 

屬性代碼和枚舉:

[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)] 
public class DisplayAttribute : Attribute 
{ 
    public DisplayAttribute(string displayName) 
    { 
     Description = displayName; 
    } 

    public string Description { get; set; } 
} 

public enum SubsAmount 
{ 
    [Display("One Year")] 
    Oneyear = 0, 
    [Display("Two Years")] 
    TwoYears = 1, 
    [Display("Three Years")] 
    ThreeYears = 2 
} 

它的樣子: here.

附:這裏你不需要任何轉換器。 此致敬禮。

+0

@ andas1951請參閱更新後的版本,這裏我使用了Display屬性。 – Ilan

+0

非常感謝你的解釋:)! – andas1951

+0

隨時將其標記爲已回答,以防情況有所幫助。 – Ilan

相關問題