自做网站打开速度慢,网上书城网站开发,网站开发实训意义,陕西省住房和建设厅官方网站此示例演示如何绑定到枚举。 遗憾的是#xff0c;没有直接方法可以将枚举用作数据绑定源。 但是#xff0c;Enum.GetValues(Type) 方法可返回值的集合。 这些值可以包装在 ObjectDataProvider 中并用作数据源。
ObjectDataProvider 类型提供了一种在 XAML 中创建对象并将其用…此示例演示如何绑定到枚举。 遗憾的是没有直接方法可以将枚举用作数据绑定源。 但是Enum.GetValues(Type) 方法可返回值的集合。 这些值可以包装在 ObjectDataProvider 中并用作数据源。
ObjectDataProvider 类型提供了一种在 XAML 中创建对象并将其用作数据源的便捷方式。使用 ObjectDataProvider 类型包装枚举类型本身提供的枚举值数组。 ObjectDataProvider x:KeyDirectionEnumDataSource ObjectType{x:Type sys:Enum} MethodNameGetValuesObjectDataProvider.MethodParametersx:Type TypeNamelocal:DirectionType/x:Type/ObjectDataProvider.MethodParameters/ObjectDataProvider
在此示例中ObjectDataProvider 使用三个属性来检索枚举
属性描述ObjectType数据提供程序要返回的对象类型。 在本示例中为 System.Enum。 sys: XAML 命名空间映射到 System。MethodName要在 System.Enum 类型上运行的方法的名称。 在本示例中为 Enum.GetValues。MethodParameters要提供给 MethodName 方法的值的集合。 在此示例中该方法采用枚举的 System.Type。
实际上XAML 正在分解方法调用、方法名称、参数和返回类型。 这里配置的ObjectDataProvider等效于一下代码
var DirectionEnumDataSource System.Enum.GetValues(typeof(EnumBinding.DirectionType));
引用ObjectDataProvider资源 ListBox ItemsSource{Binding Source{StaticResource DirectionEnumDataSource}} SelectedIndex0/ComboBox ItemsSource{Binding Source{StaticResource DirectionEnumDataSource}} SelectedIndex0/
或通过代码的方式 ListBox x:NameMyListBox/ListBoxComboBox x:NameMyComboBox/ComboBox MyListBox.ItemsSource System.Enum.GetValues(typeof(DirectionType));MyComboBox.ItemsSource System.Enum.GetValues(typeof(DirectionType));
完整测试代码
MainWindow.xaml
Window x:ClassEnumBinding.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:EnumBindingxmlns:sysclr-namespace:System;assemblymscorlibmc:IgnorabledTitleMainWindow Height450 Width800Window.ResourcesObjectDataProvider x:KeyDirectionEnumDataSource ObjectType{x:Type sys:Enum} MethodNameGetValuesObjectDataProvider.MethodParametersx:Type TypeNamelocal:DirectionType/x:Type/ObjectDataProvider.MethodParameters/ObjectDataProvider/Window.ResourcesStackPanelListBox ItemsSource{Binding Source{StaticResource DirectionEnumDataSource}} SelectedIndex0/ComboBox ItemsSource{Binding Source{StaticResource DirectionEnumDataSource}} SelectedIndex0/ListBox x:NameMyListBox/ListBoxComboBox x:NameMyComboBox/ComboBox/StackPanel
/WindowMainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace EnumBinding;public enum DirectionType : int
{Buy 0,Sell 1,
}public partial class MainWindow : Window
{public MainWindow(){InitializeComponent();MyListBox.ItemsSource System.Enum.GetValues(typeof(DirectionType));MyComboBox.ItemsSource System.Enum.GetValues(typeof(DirectionType));}
}