简体   繁体   中英

How to use an ObjectDataProvider to bind an enum to a ComboBox in XAML

I am trying to bind an Enum to a ComboBox . I have seen a lot of people using an ObjectDataProvider but I cannot seem to access it. I have also noticed that some people are using it within a Window.Resources , rather than Page.Resources but I cannot find how it is used on Page.Resources . I have been searching for a solution for hours.

What I have so far:

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);
    }
}

Here is an example with a page object(according to the MSDN documentation there is no any restriction to use the ObjectDataProvider with page):

Update #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>

Here is a custom Data provider code

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

Attribute code and enum:

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

How it looks like: 这里。

PS You don't need any converters here. Regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM