Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF combobox bind
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; }
            set
            {
                if (value < 1 || value > 3) { throw new OverflowException(); }
                m_choise = value; OnPropertyChanged("Choise");
            }
        }
    }
 
    public partial class MainWindow : Window
    {
        MyModel Model1 = new MyModel();
        public MainWindow()
        {
            this.InitializeComponent();
            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"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TempProject"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ObjectDataProvider x:Key="MyDict"
                            ObjectType="{x:Type local:Dictionaries}"
                            MethodName="GetChoise"/>
    </Window.Resources>
    <StackPanel >
            <Menu>
                <MenuItem Header="File">
                    <MenuItem Header="Open" Click="CmdOpenXML" />
                </MenuItem>
            </Menu>
            <TextBox Text="{Binding Choise}" />
            <ComboBox ItemsSource="{Binding Source={StaticResource MyDict}}" 
                      SelectedValuePath="Key" 
                      DisplayMemberPath="Value" 
                      SelectedItem="{Binding Path=Choise,Mode=TwoWay}"/>
    </StackPanel>
</Window>
 
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 23:17pm
g_ap360

1 solution

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

Solution 1

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

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



Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 2 Feb 2013
Copyright © CodeProject, 1999-2014
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