`

wpf - specify enum values in xaml

    博客分类:
  • WPF
wpf 
阅读更多

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.

 

 

分享到:
评论

相关推荐

    通用WPF主题控件包rubyer-wpf-master

    通用WPF主题控件包rubyer-wpf-master是一款专为Windows Presentation Foundation (WPF) 应用程序设计的开源UI框架。它提供了丰富的主题和控件,旨在帮助开发者快速构建美观且用户友好的应用程序界面。在2.0.0版本中...

    wpf-mvvm-DeskTop-Sample-master_C#_WPF_wpf客户端zfs_

    标题中的“wpf-mvvm-DeskTop-Sample-master”表明这是一个关于WPF(Windows Presentation Foundation)桌面应用程序的示例项目,使用了MVVM(Model-View-ViewModel)设计模式。这个项目是用C#编程语言编写的,面向的...

    bootstrap-wpf-style-master 样式

    在 Bootstrap-WPF 项目中,开发者会使用 XAML 来引用 Bootstrap 的 CSS 和 JavaScript 文件,并应用对应的类和属性来实现相应的样式效果。 2. **CSS**:Bootstrap 的核心在于其 CSS 样式,它定义了各种预设的布局、...

    Navigation-Drawer-Sidebar-Menu-in-WPF-master.zip

    标题"Navigation-Drawer-Sidebar-Menu-in-WPF-master.zip"提示我们这是一个关于WPF应用的项目,它包含了一个可展开和收缩的左侧导航菜单。描述中的“WPF左侧展开收缩图标导航菜单”进一步确认了这一功能。通过分析...

    C# WPF MaterialDesignInXAML样式库和控件集 源码

    《C# WPF MaterialDesignInXAML:打造现代UI风格的源码解析》 MaterialDesignInXamlToolkit是一个流行的开源库,它为Windows Presentation Foundation(WPF)应用程序提供了丰富的Material Design风格的控件和样式...

    wpf---StatusBar

    “wpf---StatusBar”这个标题表明我们将探讨的是WPF(Windows Presentation Foundation)框架中的StatusBar组件。WPF是.NET Framework的一部分,用于构建桌面应用程序,它提供了丰富的用户界面(UI)功能。StatusBar...

    OpenControls.Wpf-master

    《OpenControls.Wpf-master:深度探索WPF框架控件》 在Windows Presentation Foundation(WPF)的世界里,开发者们能够创建出美观且功能丰富的桌面应用程序。OpenControls.Wpf-master项目,正如其名,是一个专注于...

    WPF-进阶视频教程(共113集)-058XAML浏览器应用程序.mp4

    WPF-进阶视频教程(共113集)-058XAML浏览器应用程序.mp4

    WPF XAML 文件格式批量修改为Utf8格式

    标题“WPF XAML 文件格式批量修改为Utf8格式”涉及到的是一个常见的问题,即开发者可能发现他们的XAML文件在升级项目时编码格式不是UTF-8,这可能导致编译错误或显示问题。UTF-8是一种广泛使用的字符编码标准,能...

    WPF-MVVM计算器

    **WPF-MVVM计算器详解** Windows Presentation Foundation (WPF) 是Microsoft开发的一种用户界面框架,用于构建桌面应用程序。在WPF中,Model-View-ViewModel(MVVM)设计模式是一种广泛采用的架构模式,它将业务...

    WPF-ScreenData.zip

    超酷WPF数据可视化,全套源程序,非常适合做数据可视化的程序员使用,WPF,XAML。 找了好久的资源,附有很多很详细的插图,是大数据时代不可缺少的可视化学习数据,仅供大家学习使用请勿用做商业用途。与大家一起...

    File-Manager-UI-Wpf-master.7z

    【标题】"File-Manager-UI-Wpf-master.7z" 涉及的是一个基于WPF(Windows Presentation Foundation)的文件管理器用户界面项目。这个压缩包包含了一个完整的开发环境,供开发者进行学习和使用。 【描述】"自用资源...

    WPF-Ribbon

    WPF-Ribbon则是专门针对WPF平台实现的Ribbon组件,它为开发者提供了在WPF应用程序中集成这种现代用户界面的能力。 1. **Ribbon布局** - Ribbon界面通常包含多个主要区域:快速访问工具栏(QAT)、主选项卡、上下文...

    c#-的WPF---MVVM例子

    在“c#-的WPF---MVVM例子”中,我们可能会看到以下关键概念: 1. **数据绑定**:WPF的强项之一就是其强大的数据绑定功能。通过数据绑定,View中的UI元素可以直接与ViewModel中的属性关联,当ViewModel中的数据改变...

    WPF-XamlPad程序

    在本篇文章中,我们将深入探讨WPF-XamlPad程序,以及与之相关的VisualTree和LogicalTree概念,还有Template的使用。 首先,让我们了解**XamlPad**。XamlPad是一个简单的编辑器,允许开发者输入纯XAML代码并即时预览...

    Fast-learning-WPF-master.zip

    这个压缩包“Fast-learning-WPF-master.zip”很可能包含了一套完整的教程或者项目源代码,旨在帮助用户快速掌握WPF技术。 WPF的核心特性包括: 1. **XAML(Extensible Application Markup Language)**:这是一种...

    WPF-MM2.zip

    标题中的"WPF-MM2.zip"表明这是一个与Windows Presentation Foundation(WPF)相关的压缩文件,而“MM2”可能指的是一个项目、模块或者版本号。由于没有具体的描述和标签,我们将基于WPF的基本概念和常见知识点进行...

    WPF-4-unleashed PDF 原书

    《WPF-4-unleashed》是一本深入探讨Windows Presentation Foundation (WPF)技术的专业书籍,专注于WPF的第四代版本。WPF是Microsoft .NET Framework的重要组成部分,它为开发人员提供了一种全新的方式来创建丰富的、...

    wpf-themes-20款.zip

    本资源“wpf-themes-20款.zip”包含了20多种精心设计的Windows Presentation Foundation(WPF)应用程序的主题皮肤样式。WPF是.NET Framework的一部分,它为开发人员提供了一种强大的图形用户界面(GUI)设计工具,...

    WPF一款自动整理XAML代码的插件

    在Windows Presentation Foundation(WPF)开发中,XAML是一种重要的标记语言,用于构建用户界面。然而,由于XAML代码的复杂性和可扩展性,手动保持代码整洁和规范可能会变得困难。为了帮助开发者解决这个问题,出现...

Global site tag (gtag.js) - Google Analytics