如何使用ObjectDataProvider将枚举绑定到XAML中的ComboBox

编程入门 行业动态 更新时间:2024-10-09 19:16:03
本文介绍了如何使用ObjectDataProvider将枚举绑定到XAML中的ComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试将Enum绑定到ComboBox.我已经看到很多人使用ObjectDataProvider,但是我似乎无法访问它.我还注意到有些人在Window.Resources而不是Page.Resources中使用它,但是我找不到在Page.Resources上如何使用它.我一直在寻找解决方案几个小时.

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="schemas.microsoft/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft/winfx/2006/xaml" xmlns:local="clr-namespace:Sports;assembly=Sports" xmlns:d="schemas.microsoft/expression/blend/2008" xmlns:mc="schemas.openxmlformats/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); } }

推荐答案

以下是带有页面对象的示例(根据MSDN 文档对于在页面上使用ObjectDataProvider没有任何限制):

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

更新#1

Xaml

<Page x:Class="PageBasedApp.MyPage" xmlns="schemas.microsoft/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft/winfx/2006/xaml" xmlns:mc="schemas.openxmlformats/markup-compatibility/2006" xmlns:d="schemas.microsoft/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 }

外观如何:

P.S.您在这里不需要任何转换器. 问候.

P.S. You don't need any converters here. Regards.

更多推荐

如何使用ObjectDataProvider将枚举绑定到XAML中的ComboBox

本文发布于:2023-11-13 04:36:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583446.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   如何使用   ComboBox   ObjectDataProvider   XAML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!