-1

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.

  • 1
    You are crating a new object for `SelectedProject` Have you tried `SelectedProject = Projects[1];` instead? – Flat Eric Mar 26 '19 at 12:26
  • You create a new `ProjectTest` object for SelectedProject, you should the existing one form the collection – Pavel Anikhouski Mar 26 '19 at 12:27
  • the marked duplicate is wrong though. The answer for the duplicate was, that `Mode=TwoWay` wasn't set, which already is the case here. – Azzarrel Mar 26 '19 at 12:29
  • You may also consider using SelectedValue and SelectedValuePath instead of SelectedItem. Assuming ProjectTest has a Name property, set `SelectedValuePath="Name"` and bind SelectedValue to a string property that selects by name. – Clemens Mar 26 '19 at 12:29
  • @Azzarrel Not if you carefully read the answer. You may however help us finding a better duplicate. This has been asked so many times... – Clemens Mar 26 '19 at 12:32
  • 1
    @Clemens You are right, I didn't read carefully enough. Although it took me a while to find, I think this quesiton might be better to mark as duplicate, as the answer explains the issue Sam is facing in much more detail: https://stackoverflow.com/questions/8330521/updating-a-combobox-selecteditem-from-code-behind – Azzarrel Mar 26 '19 at 13:08
  • Apologies for the duplicate. I looked at lots of past posts expecting something to be wrong with the XAML not the Property binding the selected item. Setting an item from the collection as per the above is correct. – Sam Brueton Mar 27 '19 at 09:10

1 Answers1

1

new ProjectTest("Project2") is not equal to any of the ProjectTest that you add to the Projects collection, unless your ProjectTest class implements IEquatable<T> to define that two projects with the same name are considered to be equal.

The other option would be to set the SelectedProject property to any of objects in Projects:

SelectedProject = Projects[1];
mm8
  • 163,881
  • 10
  • 57
  • 88