Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
4.00/5 (1 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:
C#
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:
XML
<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

1 solution

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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900