Click here to Skip to main content
13,086,019 members (84,156 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I'm new to C# and WPF and my question may be too easy to some of you:
My prog crates a class member and sets its parameters reading any local xml. One of the parameters is an Int parameter. I want a combobox to handle this parameter but to show a string. My approach is to create a dictionary witch populates the combo and bind its selected value from the class member:
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;
using System.ComponentModel;
using System.Xml;
using System.Xml.Linq;
namespace TempProject
    class Dictionaries
        public static Dictionary<int, string> GetChoise()
            Dictionary<int, string> MyDict = new Dictionary<int, string>();
            MyDict.Add(1, "choise 1");
            MyDict.Add(2, "choise 2");
            MyDict.Add(3, "choise 3");
            return MyDict;
    public class MyModel : INotifyPropertyChanged
        private int m_choise;
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        public int Choise
            get { return m_choise; }
                if (value < 1 || value > 3) { throw new OverflowException(); }
                m_choise = value; OnPropertyChanged("Choise");
    public partial class MainWindow : Window
        MyModel Model1 = new MyModel();
        public MainWindow()
            DataContext = Model1;
        private void CmdOpenXML(object sender, System.Windows.RoutedEventArgs e)
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.InitialDirectory = @"C:\";
            dlg.DefaultExt = ".xml";
            dlg.Filter = "XML|*.xml";
            Nullable<bool> result = dlg.ShowDialog();
            if (result == true)
                XDocument xd = XDocument.Load(dlg.FileName);
                var query = from d in xd.Root.Descendants("PROJECT")
                            select d;
                foreach (var q in query)
                { Model1.Choise = Int32.Parse(q.Element("choise").Value); }

and the XAML:
<Window x:Class="TempProject.MainWindow"
        Title="MainWindow" Height="350" Width="525">
        <ObjectDataProvider x:Key="MyDict"
                            ObjectType="{x:Type local:Dictionaries}"
    <StackPanel >
                <MenuItem Header="File">
                    <MenuItem Header="Open" Click="CmdOpenXML" />
            <TextBox Text="{Binding Choise}" />
            <ComboBox ItemsSource="{Binding Source={StaticResource MyDict}}" 
                      SelectedItem="{Binding Path=Choise,Mode=TwoWay}"/>

But the combo does not work correctly.
Can you tell me what am I doing wrong? Is my approach correct in general?
Can you suggest me some tutorials on the subject?
Thank you
Posted 29-Jan-13 22:17pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

It was simple after all. Should have used SelectedValue instead of SelectedItem in ComboBox

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170813.1 | Last Updated 2 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100