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.


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”
     Title=”Elements” Height=”150″ Width=”300″
    <StackPanel HorizontalAlignment=”Left” Margin=”10″ Background=”Yellow”>
             ItemsSource=”{Binding DropDownItem}”
             SelectedValue=”{Binding ValSelected}”

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
                return _valueSelected;

                _valueSelected = value;

        public ObservableCollection<KeyValuePair<string, string>> DropDownItem
                return _dropDownValues;

                _dropDownValues = value;

        public ComboBox()
            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

Comments (2)

  1. YogiBear says:

    Sriously President Obama must have more serious things to do then write crappy programs.

  2. Surf4Fun says:

    YogiBear, I think you are correct, but it would be a good model for inner city schools to follow.

    I think that programming is a great hobby.

    Apparently you must read really fast as you posted this instantly after I punched this guy up.

    If you are in my class at CSUDH, use your real name and ask some harder questions.