Lindsay
Lindsay

Reputation: 595

binding combobox items to dictionary of enums

still pretty new at WPF and binding especially, but I have an enum which I will be using as properties on objects elsewhere in my project, but one of the very first kickoff points of the program will be the user selecting a single item from a combobox, which I want to match to the available enum options. I originally thought to have a dictionary object with the enum option as the key, and the value as a string for use in the UI presentation, and this is what I have been working towards. I have been searching around and thought I had it, but the combobox is populating blank.

I have a couple of questions;

Firstly, since I'm still not quite sure what is what in relation to binding, is this issue related to this post Target Exception Bug which I found in a comment on another question? If so, does this mean I'm barking up the wrong tree for the time being? And is there another way for me to achieve my goal?

Secondly, if its not related, have I missed something in the below code? I currently get no error in the output window and the project compiles fine.

Here's the enum (which lives in a separate namespace that has been added to the project references);

namespace WGM_lbr
{
public class Available_Wgms
{
    private static Dictionary<Wgms,string> _wgmColl;
    public static Dictionary<Wgms,string> WgmsCollection 
    {
        get 
        {
            return _wgmColl;
        }
    }

    static Available_Wgms()
    {
        _wgmColl = new Dictionary<Wgms, string>() {
            {Wgms.First, "First Dictionary item"},
        //other Dictionary Items go here
        }
    }

    public enum Wgms
    {
        First,
        //other Enum options go here
    }
}
}

My Resource declaration in app.xaml

<Application x:Class="The_First.App"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:WGM="clr-namespace:WGM_lbr;assembly=WGM_lbr"
         StartupUri="MainWindow.xaml">
<Application.Resources>
    <BooleanToVisibilityConverter x:Key="b2v"/>
    <WGM:Available_Wgms x:Key="WgmList"/>
</Application.Resources>

And finally the combobox and binding xaml (in case its relevant, this lives in a nest of wpf controls, up to a grid which lives on page, which is being loaded to mainwindow.xaml via a frame control using the page as the source. Both the page and mainwindow have declared the WGM namespace - I cut these out as this post is already long enough);

<ComboBox Name="cmbWgmSelector" Margin="5,0" ItemsSource="{Binding Source={StaticResource WgmList}}"/>

Any help/advice that can be provided is greatly appreciated.

Upvotes: 1

Views: 688

Answers (1)

Nitin Purohit
Nitin Purohit

Reputation: 18580

Update the binding as below:

<ComboBox Name="cmbWgmSelector" Margin="5,0" DisplayMemberPath="Value" ItemsSource="{Binding Path=WgmsCollection, Source={StaticResource WgmList}}"/>

Upvotes: 1

Related Questions