Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Binding ComboBox to enum
Message
General information
Forum:
ASP.NET
Category:
Windows Presentation Foundation (WPF)
Miscellaneous
Thread ID:
01559922
Message ID:
01560292
Views:
35
>Hi Frank,
>
>You can create ObservableCollection< > or List< > instance from the enum, and bind your combo to it. Below is a sample.
>
>XAML
>
>< Window x:Class="ComboboxToEnumBinding.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">
>    
>    < Grid>
>        < ComboBox HorizontalAlignment="Center" 
>                  VerticalAlignment="Center" 
>                  Width="180" 
>                  ItemsSource="{Binding AvailableOptions}" 
>                  SelectedValue="{Binding SelectedOption}" />
>    < /Grid>
>< /Window>
>
>
>Code-behind:
>
>namespace ComboboxToEnumBinding
>{
>    using System;
>    using System.Collections.ObjectModel;
>
>    public enum OptionsEnum
>    {
>        Standard = 1,
>        Mail = 2,
>        ZipX = 3
>    }
>
>    public partial class MainWindow
>    {
>        private OptionsEnum _selectedOption = OptionsEnum.Standard;
>
>        private ObservableCollection<OptionsEnum> _availableOptions;
>
>        public ObservableCollection<OptionsEnum> AvailableOptions
>        {
>            get
>            {
>                if (this._availableOptions == null)
>                {
>                    this._availableOptions = new ObservableCollection<OptionsEnum>();
>                    foreach (OptionsEnum option in Enum.GetValues(typeof(OptionsEnum)))
>                    {
>                        this._availableOptions.Add(option);
>                    }
>                }
>
>                return this._availableOptions;
>            }
>
>            set
>            {
>                this._availableOptions = value;
>            }
>        }
>
>        public OptionsEnum SelectedOption
>        {
>            get
>            {
>                return this._selectedOption;
>            }
>
>            set
>            {
>                this._selectedOption = value;
>            }
>        }
>
>        public MainWindow()
>        {
>            InitializeComponent();
>            this.DataContext = this;
>        }
>    }
>}
>
Not sure an ObservableCollection serves much purpose with an enum - after all the enum is not going to change during execution ?
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform