wpf - specify enum values in xaml

本文详细介绍了如何在XAML文件中指定枚举值,包括直接在命名空间根目录中指定枚举文字、指定定义在外部类中的枚举文字以及如何通过ObjectDataProvider获取所有枚举值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值