ComboBox: Binding data to the ComboBox using INotifyPropertyChanged

 

I am using Visual Studio C# Express for this example, the better debugging tool is Visual Studio Ultimate, if you are a student you should use that instead of Express.  Hopefully if President Obama is looking for hobby to show off to high school students, he will take up programming using Visual Studio Express.  Cheap and fun.

The code for this application likely was taken initially from some site, if you recognize it as your code, let me know so I can reference your work.

To get started , when we launch the project in debug mode, with a breakpoint set at the initial property, you can have show the variable setting and pin it, which I did in my code here.

image

So much for the action shot.

In the XAML, the ComboBox, ItemSource is bound to DropDownItem in the C# Code..  The image shown is one of the debug tools in Visual Studio C# 2010 Express.

Note in the XAML the combo box ItemsSource is bound to the DropDownItem in C#.  The SelectedValue is bound to the ValSelected.

I have attached the working code, it works in Visual Studio 2010 C# Express which means that it is operational in Framework 4.0.  It was working when I zipped it up, if it isn’t working, please leave a comment, and I will fix it.

The working code is shown below (and this definitely code written by someone else, but I am unable to determine who):

Here is the XAML:

<Window x:Class="TestComboObserver.ComboBox"
     xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
     Title="Elements" Height="150" Width="300"
     Background="Red">
    <StackPanel HorizontalAlignment="Left" Margin="10" Background="Yellow">
        <ComboBox
             DisplayMemberPath="Value"
             ItemsSource="{Binding DropDownItem}"
             SelectedValue="{Binding ValSelected}"
             SelectedValuePath="Key"
             Width="200"
         />
    </StackPanel>
</Window>

Here is the C# code:

using System.Windows;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Collections.Generic;

namespace TestComboObserver
{
    public partial class ComboBox : INotifyPropertyChanged
    {
        private ObservableCollection<KeyValuePair<string, string>> _dropDownValues =
                                    new ObservableCollection<KeyValuePair<string, string>>();

        private string _valueSelected;

        public event PropertyChangedEventHandler PropertyChanged;

        public string ValSelected
        {
            get
            {
                return _valueSelected;
            }

            set
            {
                _valueSelected = value;
                OnPropertyChanged("ValSelected");
            }
        }

        public ObservableCollection<KeyValuePair<string, string>> DropDownItem
        {
            get
            {
                return _dropDownValues;
            }

            set
            {
                _dropDownValues = value;
                OnPropertyChanged("DropDownItem");
            }
        }

        public ComboBox()
        {
            InitializeComponent();
            DataContext = this;

            DropDownItem.Add(new KeyValuePair<string, string>("1000", "Aluminum"));
            DropDownItem.Add(new KeyValuePair<string, string>("2000", "Boron"));
            DropDownItem.Add(new KeyValuePair<string, string>("3000", "Carbon"));
           

            ValSelected = "3000";
        }

        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;

            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

Databinding simple combo box.zip