How do I bind a WPF ComboBox to an enumerator in another class?

How do I bind a WPF ComboBox to an enumerator in another class?

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Diagnostics;
using System.Text;
using FirebirdSql.Data.FirebirdClient;
using System.IO;
using System.Data;
using log4net;

namespace WPFLabelDatabindUpdateSample
{    
    public class ChronDatabasesList : ObservableCollection<ChronDatabases>
    {
        public ChronDatabasesList()
            : base()
        {
            Add(new ChronDatabases());
        }
    }

    public class ChronDatabases : INotifyPropertyChanged
    {
        public enum TargetDB { Primary, Secondary, Archive }
        private string _PrimaryDBPath = string.Empty;
        public string PrimaryDBPath
        {
            get { return _PrimaryDBPath; }
            set
            {
                if (_PrimaryDBPath != value)
                {
                    Utils.SetRegistryValue("SOFTWARE\\Chronicle\\Databases", "Primary", value);                    
                    _PrimaryDBPath = value;
                    OnPropertyChanged("PrimaryDBPath");
                }
            }
        }

        public enum ServerUpdateGroup
        {
            alpha = 1,
            beta = 2,
            beta2 = 3,
            cmobile = 4,
            cweb = 5,
            cwebmobile = 6,
            test = 7,
            triage = 8,
            zone1 = 9,
            zone2 = 10,
            zone3 = 11,
            zone4 = 12
        }
        private ServerUpdateGroup _UpdateGroup;
        public ServerUpdateGroup UpdateGroup
        {
            get { return _UpdateGroup; }
            set
            {
                //Utils.SetServerSettingValue("SMTP Authentication", value.BoolToYN());
                Utils.SetRegistryValue("SOFTWARE\\Chronicle\\Databases", "UpdateZone", value);
                _UpdateGroup = value;
            }
        }

        public ChronDatabases()
        {
            try
            {
                _PrimaryDBPath = Utils.GetRegistryValue("SOFTWARE\\Chronicle\\Databases", "Primary", string.Empty).ToString();
                _UpdateGroup = (ServerUpdateGroup)Enum.Parse(typeof(ServerUpdateGroup), Utils.GetRegistryValue("SOFTWARE\\Chronicle\\Databases", "UpdateZone", "Not Set").ToString(), true);
            }
            catch (Exception ex)
            {
                EventLog.WriteEntry("ChronBackups-Databases", ex.Message, EventLogEntryType.Error, 49);
            }
        }

        // Create the OnPropertyChanged method to raise the event
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

Open in new window


I am using a VS2012 and making a WPF application.  I am currently working through the details.  I am trying to Databind:

public enum ServerUpdateGroup
        {
            alpha = 1,
            beta = 2,
            beta2 = 3,
            cmobile = 4,
            cweb = 5,
            cwebmobile = 6,
            test = 7,
            triage = 8,
            zone1 = 9,
            zone2 = 10,
            zone3 = 11,
            zone4 = 12
        }

Open in new window


This to a ComboBox and I am unable to create a CollectionViewSource for the enumerator.  I am not even sure that is the way to go.  Here is the rest of my current code:

MainWindow.xaml

<Window
        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:WPFLabelDatabindUpdateSample" 
        mc:Ignorable="d" x:Class="WPFLabelDatabindUpdateSample.MainWindow"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Window.Resources>
        <CollectionViewSource x:Key="chronDatabasesViewSource" d:DesignSource="{d:DesignInstance {x:Type local:ChronDatabases}, CreateList=True}"/>
        <CollectionViewSource x:Key="serverUpdateGroupEnumViewSource" x:Shared="False">
            <CollectionViewSource.Source>
                <ObjectDataProvider 
                        MethodName="GetValues"  
                        ObjectType="{x:Type sys:Enum}">
                    <ObjectDataProvider.MethodParameters>
                        <x:Type TypeName="local:ServerUpdateGroup" />
                    </ObjectDataProvider.MethodParameters>
                </ObjectDataProvider>
            </CollectionViewSource.Source>
        </CollectionViewSource>
    </Window.Resources>
    <Grid>

        <StackPanel x:Name="grid1" Margin="10,10,10,0" VerticalAlignment="Top" Height="300">
            <Button x:Name="btnChangeValue" Content="Change Value" HorizontalAlignment="Left" Margin="52,37,0,0" VerticalAlignment="Top" Width="99" Click="btnChangeValue_Click"/>
            <Label Content="{Binding PrimaryDBPath, Source={StaticResource chronDatabasesViewSource}}" Margin="52,30,10,-51" />
            <ComboBox
                        ItemsSource="{Binding ServerUpdateGroup, Source={StaticResource serverUpdateGroupEnumViewSource}}"
                        SelectedItem="{Binding UpdateGroup, Source={StaticResource chronDatabasesViewSource}}">
            </ComboBox>
        </StackPanel>
        
    </Grid>
</Window>

Open in new window


Code Behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPFLabelDatabindUpdateSample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private ChronDatabasesList _cdl = new ChronDatabasesList();

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            System.Windows.Data.CollectionViewSource chronDatabasesViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("chronDatabasesViewSource")));
            // Load data by setting the CollectionViewSource.Source property:
            chronDatabasesViewSource.Source = _cdl;            
        }


        private void btnChangeValue_Click(object sender, RoutedEventArgs e)
        {
            var item = _cdl.FirstOrDefault();
            item.PrimaryDBPath = "Hello Again!";
        }
    }
}

Open in new window


I am not sure I am headed in the right direction.  Any guidance would be greatly appreciated.
Todd710Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BrWadaCommented:
I like doing things on code-behind so I removed all the stuff on the combobox and gave it a name:

<ComboBox x:Name="cboBox"></ComboBox>

Open in new window



Then I set the ItemsSource property on Window_Loaded:

private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ...
            cboBox.ItemsSource = Enum.GetValues(typeof(ServerUpdateGroup));
        }

Open in new window



Enum.GetValues will turn your Enum into an array so you can bind to it.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Todd710Author Commented:
I was hoping to do it in XAML but your approach make way to much sense.  Thanks for the timely help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.