I am trying to bind a combo box selected item to an object on the View model. It binds the itemsource and will save the selected item, however if i populate a value of selected item on load it will not show.
Here is the XAML of the ComboBox:
<ComboBox Name="cbxProjects"
ItemsSource="{Binding Projects}"
DisplayMemberPath="Name"
SelectedItem="{Binding Path=SelectedProject, Mode=TwoWay}"
Here are the Classes I am binding to:
private Collection<ProjectTest> projects;
public Collection<ProjectTest> Projects
{
get { return projects; }
set
{
projects = value;
RaisePropertyChangedEvent("Projects");
}
}
private ProjectTest selectedProject;
public ProjectTest SelectedProject
{
get { return selectedProject; }
set
{
selectedProject = value;
RaisePropertyChangedEvent("SelectedProject");
}
}
Here is my initializer of the view model:
public MyViewModel(ProjectHelper projectHelper)
{
Projects = new Collection<ProjectTest>();
Projects.Add(new ProjectTest("Project1"));
Projects.Add(new ProjectTest("Project2"));
Projects.Add(new ProjectTest("Project3"));
SelectedProject = new ProjectTest("Project2");
}
When I run this I expect the Combo Box to have 3 projects in the drop down and 'Project2' already selected.
This is not the case as nothing is selected.