ComboBox: Binding data to the ComboBox using INotifyPropertyChanged

Translate This Page

ComboBox: Binding data to the ComboBox using INotifyPropertyChanged

  • Comments 2

 

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="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://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));
            }
        }
    }
}

Attachment: Databinding simple combo box.zip
Leave a Comment
  • Please add 6 and 4 and type the answer here:
  • Post
  • Sriously President Obama must have more serious things to do then write crappy programs.

  • 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.

Page 1 of 1 (2 items)
Generic Content

Legal Note:

  1. The author, Sam Stokes, is an employee of Microsoft
  2. There is no guarantee that anything I write is correct, I do try to make sure that what I write is correct.
  3. Use anything written in this blog at your own risk
  4. Test what I write about before using the information
  • Sometimes posts will disappear because I realized that they really don't fit with the total chaos of what I write about
  • Links are tested and available on date of publication, what others do with their links are out of my control

Restrictions:

  • Whatever I write on this blog is unedited by management or anyone, which should be obvious from the randomness of the blog.  I simply write about things I like.
  • Microsoft does not restrict me in anyway, so I generallyblather about whatever I want to.
  • For personal reasons I will not write about:
    • Company confidential stuff, mainly because Microsoft is more transparent than any company I have ever worked for.
    • Other people unless they give me permission to write about them