WPF ComboBox Binding Enum

Source: Internet
Author: User

Don't say anything, look at the code first

Enumeration:

namespace wpfapptest{    publicenum  Week    {        Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday    }}

Page:

<Windowx:class= "Wpfapptest.mainwindow"xmlns= "Http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x= "Http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d= "http://schemas.microsoft.com/expression/blend/2008"XMLNS:MC= "http://schemas.openxmlformats.org/markup-compatibility/2006"Xmlns:sys= "Clr-namespace:system;assembly=mscorlib"xmlns:local= "Clr-namespace:wpfapptest"mc:ignorable= "D"Title= "MainWindow"Height= " the"Width= "525">    <Grid>        <grid.resources>            <ObjectDataProviderx:key= "Nameweek"MethodName= "GetNames"ObjectType="{x:type Sys:enum}">                <objectdataprovider.methodparameters>                    <x:typeTypeName= "Local:week"/>                </objectdataprovider.methodparameters>            </ObjectDataProvider>            <ObjectDataProviderx:key= "Valueweek"MethodName= "GetValues"ObjectType="{x:type Sys:enum}">                <objectdataprovider.methodparameters>                    <x:typeTypeName= "Local:week"/>                </objectdataprovider.methodparameters>            </ObjectDataProvider>        </grid.resources>        <ComboBoxSelectedValue="{Binding Selectedweek}"ItemsSource="{Binding Source={staticresource Nameweek}}"HorizontalAlignment= "Left"Height= " the"Margin= "38,40,0,0"VerticalAlignment= "Top"Width= "Max"/>        <ComboBoxSelectedValue="{Binding Selectedweek}"ItemsSource="{Binding Source={staticresource Valueweek}}"HorizontalAlignment= "Left"Height= " the"Margin= "288,40,0,0"VerticalAlignment= "Top"Width= "Max"/>    </Grid></Window>

Background:

usingSystem.Windows;namespacewpfapptest{/// <summary>    ///the interactive logic of MainWindow.xaml/// </summary>     Public Partial classMainwindow:window { PublicMainWindow () {InitializeComponent (); DataContext= This; }         PublicWeek Selectedweek {Get{return(Week) GetValue (selectedweekproperty);} Set{SetValue (selectedweekproperty, value);} }         Public Static ReadOnlyDependencyProperty Selectedweekproperty =Dependencyproperty.register ("Selectedweek",typeof(Week),typeof(MainWindow),NewPropertyMetadata (default(Week))); }}

Before looking at the online examples are used GetNames reflection, but tied SelectedValue but only from the interface to the backstage, later saw the code of the enum to understand to use GetValues

System.Enum:

 Public Static string [] GetNames (Type enumtype);  Public Static Array GetValues (Type enumtype);

WPF ComboBox Binding Enum

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.