2016-05-07 5 views
0

EnumComboBoxにバインドしようとしています。私はObjectDataProviderを使用している多くの人々を見ましたが、私はそれにアクセスできないようです。私はまた、Page.ResourcesではなくWindow.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]と、間違ったことの_precise_の説明を提供してください。 –

答えて

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; 
    } 
} 
です 属性コードと列挙型: here.

P.S.:それはどのように見えるか

[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 
} 

ここにコンバーターは必要ありません。よろしくです。 よろしくお願いします。

+0

@ andas1951更新されたバージョンをご覧ください。ここでは、表示属性を使用しました。 – Ilan

+0

ありがとうございました! – andas1951

+0

は、役に立つと思われる場合には、これを回答としてマークしてください。 – Ilan

関連する問題