Many a situation you find that you are in wantting to put some enum values into the xaml files, there are basically two ways.. one is the Property element where you can directly create values of the Enum by some type of conversion.
<local:MyEnum>Value1</local:MyEnum>
and another is the use of x:Static construct, where it looks like this:
<x:Static Member="local:MyEnum.Value2" />
at places where the x:StaticExtension is supported, you can try to do this
<... Value="{x:Static local:MyEnum.Value1" />
Here we will show you the following
- how to specicy the enum literal in xaml for enum directly in the namespace root
- how to specify enum literal for enum defined inside some outer classes
- how to get all values of enums. with help of ObjectDataProvider.
First, let see how normally you can specify the enum literals.
<x:Array Type="{x:Type local:MyEnum}" x:Key="EnumValuesSource"> <local:MyEnum>Value1</local:MyEnum> <x:Static Member="local:MyEnum.Value2" /> <local:MyEnum>Value3</local:MyEnum> </x:Array>
and you can make a ObjectDataProvider as such
<ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="EnumValuesSource3" > <ObjectDataProvider.MethodParameters> <x:TypeExtension Type="{x:Type local:MyEnum}" /> </ObjectDataProvider.MethodParameters> </ObjectDataProvider>
If only we can do that Property attribute syntax, it will allow us to do the following.
<x:Type x:Key="typeToPassIn" TypeName="local:MyEnum" /> <local:MyEnum x:Key="valueToPassIn">Value1</local:MyEnum> <ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="ExampleEnumValues" MethodParameters="{StaticResource valueToPassIn}"> </ObjectDataProvider>
however, we have to use the Property element sytax, which looks like above.
this is a side note, when you creat a x:Type object, you can use the TypeName or the x:type markup extension, however, according to the test, WPF does not support (maybe the ObjectDataProvider does not support) that type of Type object.
with type property
<x:Type x:Key="typeToPassIn2" Type="{x:Type local:MyEnum}" />
with typeName property
<x:Type x:Key="typeToPassIn" TypeName="local:MyEnum" />
What if the Enum is nested inside another class, who can we use that in Xaml code? You will need to use like this :
<x:Array Type="{x:Type local:OuterClass+MyEnum}" x:Key="EnumInAnotherClass"> <x:Static Member="local:OuterClass+MyEnum.Value1" /> <x:Static Member="local:OuterClass+MyEnum.Value2" /> <x:Static Member="local:OuterClass+MyEnum.Value2" /> </x:Array>
Notice the '+' sign in the middle of the containing class and the enum inside.
Now let's see the code that we have created for an example ..
// MyEnum.cs namespace enumInXamldemo { public enum MyEnum { Value1, Value2, Value3 } }
and the outerClass.cs
// OuterClass.cs public class OuterClass { public enum MyEnum { Value1, Value2, Value3 } }
Last is the MainWindow.xaml file
<Window x:Class="enumInXamldemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" xmlns:local="clr-namespace:enumInXamldemo" xmlns:sys="clr-namespace:System;assembly=mscorlib" > <Grid> <Grid.Resources> <x:Array Type="{x:Type local:MyEnum}" x:Key="EnumValuesSource"> <local:MyEnum>Value1</local:MyEnum> <x:Static Member="local:MyEnum.Value2" /> <local:MyEnum>Value3</local:MyEnum> </x:Array> <!-- Check on the answer from: rudigrobler http://stackoverflow.com/questions/3751461/wpf-enumeration-value-as-objectdataproviders-method-parameter --> <!-- Becareful of the pitfall the typename is not well supported in all XAML families, it does not work in the EnumValuesSource2 shown below. --> <x:Type x:Key="typeToPassIn" TypeName="local:MyEnum" /> <local:MyEnum x:Key="valueToPassIn">Value1</local:MyEnum> <!-- this won't work because Error 1 'MethodParameters' property is read-only and cannot be set from markup. Line 29 Position 7. C:\dev\workspaces\dotnet\microsoft\wpf\src\assemblies\enumInXaml\enumInXamldemo\MainWindow.xaml 29 7 enumInXamldemo --> <!-- <ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="ExampleEnumValues" MethodParameters="{StaticResource valueToPassIn}"> </ObjectDataProvider>--> <!-- Inline the MethodParameters because ObjectDataProvider does not support it in the Property attribute --> <ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="EnumValuesSource2" > <ObjectDataProvider.MethodParameters> <StaticResourceExtension ResourceKey="valueToPassIn" /> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> <!-- Since "ExampleEnumValues" shown above does not work with property attribute, we changed to use Property Element --> <ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="EnumValuesSource3" > <ObjectDataProvider.MethodParameters> <x:TypeExtension Type="{x:Type local:MyEnum}" /> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> <!-- You can also improve the way in EnumValueSoruce2, but this time use "Type" member. --> <x:Type x:Key="typeToPassIn2" Type="{x:Type local:MyEnum}" /> <ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="EnumValuesSource4" > <ObjectDataProvider.MethodParameters> <StaticResourceExtension ResourceKey="typeToPassIn2" /> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> <!-- Pass Enum values when the Enum is from another containing class http://stackoverflow.com/questions/359699/passing-an-enum-value-as-command-parameter-from-xaml see the answer from "tbergelt" --> <!-- In case that the Enum is defined inside another class such as the enumInXamldemo.OuterClass.MyEnum --> <x:Array Type="{x:Type local:OuterClass+MyEnum}" x:Key="EnumInAnotherClass"> <x:Static Member="local:OuterClass+MyEnum.Value1" /> <x:Static Member="local:OuterClass+MyEnum.Value2" /> <x:Static Member="local:OuterClass+MyEnum.Value2" /> </x:Array> </Grid.Resources> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <ComboBox x:Name="EnumComboBox" ItemsSource="{Binding Source={StaticResource EnumInAnotherClass}}" /> </Grid> </Window>
References.
- specifying an enum value from within XAML
- WPF enumeration value as ObjectDataProvider's method parameter
- Passing an enum value as command parameter from xaml
相关推荐
gong-wpf-dragdrop, GongSolutions.WPF.DragDrop 库是WPF的拖动'n'拖放框架 简介GongSolutions.WPF.DragDrop 库是一个易于使用的拖拉'n'拖放框架。特性使用 MVVM: 拖放逻辑可以放在ViewModel中。 代码不需要放在in中...
c#-的WPF---MVVM例子,大家共同学习
一个简单的WPF程序
bootstrap-wpf-style-master 样式代码 。。。。。。。。
C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手
Prism-Samples-Wpf-master06-10的VS2017版本实现,下载手动重新安装一下nuget包即可,方便大家学习
wpf--ListBox分组-排序 wpf--ListBox分组-排序
Pro-WPF-in-C-Sharp-2008-Windows-Presentation-Foundation-with-NET-3-5-2Ed.pdf
我最近打算学习WPF ,在寻找MVVM框架的时候发现了PRISM,在此之前还从一些博客上了解了其它的MVVM框架,比如浅谈WPF中的MVVM框架--MVVMFoundation 中提到的MVVMFoundation,再比如 ViewModel从未如此清爽 - 轻量级...
wpf-wcf-wf整合开发实例--有利于理解wpf-wcf-wf的整合
WPF收集程序
WPF的基本空间历程,使用.net core3.0.1版本
WPF-进阶视频教程(共113集)-058XAML浏览器应用程序.mp4
WPF--Effect特效 对Effect的进行使用的小例子
wpf-datagrid-access DB,可以更新数据到Access数据库
wpf-资源的使用-resource
超酷WPF数据可视化,全套源程序,非常适合做数据可视化的程序员使用,WPF,XAML。 找了好久的资源,附有很多很详细的插图,是大数据时代不可缺少的可视化学习数据,仅供大家学习使用请勿用做商业用途。与大家一起...
Wpf-图片放大缩小移动操作 Wpf-图片放大缩小移动操作
wpf charts源码,D源码,图书:practical-wpf-charts-graphics-master
C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手