0

I am having a problem with databinding on a ComboBox. The combobox does not show initially set items.

My .xaml:

<ComboBox 
    Name="LeaderTextBox" 
    Grid.Column="6" 
    Grid.Row="2"  
    MinWidth="150" 
    SelectedItem="{Binding Path=ProjectLeader}" 
    DisplayMemberPath="Name" Margin="5" 
    />

In the code I set the ItemSource like this:

LeaderTextBox.ItemsSource = service.GetAllEmployee();

service.GetAllEmployee() returns a list/array of employees and they are also filled into the combobox, meaning I can select them.

My problem is that if I have a value set for ProjectLeader, then it is not displayed. What am I missing there?


Model for reference:

[Serializable]
[DataContract(Namespace = "Shared", IsReference = true)]
public class Employee
{
    public Employee()
    {
        this.Projects = new List<Project>();
    } 

    [DataMember]
    public int? ID { get; set; }

    [DataMember]
    public String Name { get; set; }

    [DataMember]
    public String JobDescription { get; set; }

    [DataMember]
    public String Department { get; set; }

    [DataMember]
    public String DirectDialing { get; set; }

    [DataMember]
    public bool Status { get; set; }

    [DataMember]
    public virtual Project LeaderOfProject { get; set; }

    [DataMember]
    public virtual List<Project> Projects { get; set; }

    public override string ToString()
    {
        return ID + " " + Name + " " + JobDescription + " " + Department + " " + DirectDialing;
    }


}
[Serializable]
[DataContract(Namespace = "Shared", IsReference = true)]
public class Project
{

    public Project()
    {
        this.EmployeesWorkingOnProject = new List<Employee>();
        this.ProjectSteps = new List<ProjectStep>();
    }

    [DataMember]
    public int? ID { get; set; }

    [DataMember]
    public String Titel { get; set; }

    [DataMember]
    public DateTime StartDate { get; set; }

    [DataMember]
    public DateTime EndDate { get; set; }

    [DataMember]
    public String Description { get; set; }

    [DataMember]
    public Employee ProjectLeader { get; set; }

    [DataMember]
    public bool Status { get; set; }

    [DataMember]
    public virtual List<Employee> EmployeesWorkingOnProject { get; set; }

    [DataMember]
    public virtual List<ProjectStep> ProjectSteps { get; set; }

    public override string ToString()
    {
        return ID +" "+ Titel+" "+StartDate+ " "+ EndDate +" "+ Description;
    }

}
XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65
  • What is `ProjectLeader`? For `SelectedItem` to work here, it should be the name of a property on your DataContext object, the value of which should be an object of type `Employee`, which **must be** contained in the collection of objects returned by `service.GetAllEmployee()`. Not just "has the same ID", but the actual instance has to be in there. The way you're populating the listbox that seems unlikely. – 15ee8f99-57ff-4f92-890c-b56153 May 14 '18 at 19:40
  • I'd advise making the employee collection a property of the viewmodel, then the viewmodel can scan through the collection for Rodolfo Dopamine or whoever, and assign his actual instance of `Employee` to `ProjectLeader`. Or else go by `SelectedValue`/`SelectedValuePath="ID"`. – 15ee8f99-57ff-4f92-890c-b56153 May 14 '18 at 19:43
  • @EdPlunkett I am sorry, but I don't really understand what you mean with `SelectedValue/SelectedValuePath="ID"`. I tried `SelectedValue="{Binding Path=ProjectLeader.ID}" SelectedValuePath="ID"` and `SelectedValue="{Binding Path=ProjectLeader}" SelectedValuePath="ID"`, but no success. Could you give me an example on how to solve it? – XtremeBaumer May 14 '18 at 19:55
  • Did you read the [documentation on ComboBox.SelectedValue](https://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.selectedvalue(v=vs.110).aspx) (Selector is a common base class for ComboBox, ListBox, etc.)? [This may help as well](https://stackoverflow.com/a/4902454/424129). You have to understand what it means. – 15ee8f99-57ff-4f92-890c-b56153 May 14 '18 at 20:01
  • @EdPlunkett I read everything and I understand the problem (the value I want to display is not the same as in the list I set as `ItemSource`), but I still have no idea on how to fix it – XtremeBaumer May 14 '18 at 20:23
  • What Employee object do you want to select initially? – mm8 May 15 '18 at 10:05
  • @mm8 either nothing if the `Project.ProjectLeader` is null, or the proper `Project.ProjectLeader` if one is set – XtremeBaumer May 15 '18 at 10:49
  • @XtremeBaumer: Where and how do you instantiate the Project class? – mm8 May 15 '18 at 13:45
  • @mm8 its always fetched from database in that case. I also have a case where I create a `new Project()` but there is no need to set the `ProjectLeader` – XtremeBaumer May 15 '18 at 13:51
  • You bind to a ProjectLeader property in your XAML...where is this property defined and set? – mm8 May 15 '18 at 13:52
  • @mm8 defined in `Project` class. Mapping is done through EF (working just fine for select) – XtremeBaumer May 15 '18 at 13:53
  • Then again, where is this instance of Project defined? Post your code if you want any help. Where do you set the value to be selected in the ComboBox? – mm8 May 15 '18 at 13:54
  • `Where do you set the value to be selected in the ComboBox` -> read the question. But why does it even matter? It is clearly stated what I want to achieve (1 POCO and a list of other POCOs as `ItemSource`) – XtremeBaumer May 15 '18 at 13:57

1 Answers1

0

You can't select an item of a combo box by an item which is not in the combo box.

Right:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var paul = new Employee { Name = "Paul" };
        LeaderTextBox.ItemsSource = new List<Employee>()
        {
            new Employee {Name = "Bob" },
            paul,
            new Employee {Name = "Alex" },
        };
        ProjectLeader = paul;
    }

Wrong:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        LeaderTextBox.ItemsSource = new List<Employee>()
        {
            new Employee {Name = "Bob" },
            new Employee { Name = "Paul" },
            new Employee {Name = "Alex" },
        };
        ProjectLeader = new Employee {Name = "Paul" };
    }
Bizhan
  • 16,157
  • 9
  • 63
  • 101