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:
Title=”Elements” Height=”150″ Width=”300″
<StackPanel HorizontalAlignment=”Left” Margin=”10″ Background=”Yellow”>
Here is the C# code:
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
_valueSelected = value;
public ObservableCollection<KeyValuePair<string, string>> DropDownItem
_dropDownValues = value;
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));