2
votes

Business Models:

public class Employe : INotifyPropertyChanged
{
    public int EmployeId { get; set; }
    public string EmployeName { get; set; }
    public Department EmployeDepartment { get; set; }
}
public class Department : INotifyPropertyChanged
{
    public int DepartmentId { get; set; }
    public string DepartmentName { get; set; }
}

List Property in MainWindow:

    public List<Department> Departments { get; set; } = new List<Department>();
    public List<Employe> Employes { get; set; } = new List<Employe>();

Sample Initialisation:

        var office = new Department { DepartmentId = 1, DepartmentName = "Office" };
        var admin = new Department { DepartmentId = 2, DepartmentName = "Administration" };
        var purchasing = new Department { DepartmentId = 3, DepartmentName = "Purchasing" };
        Departments.Add(office);
        Departments.Add(admin);
        Departments.Add(purchasing);
        Employes.Add(new Employe { EmployeId = 1, EmployeName = "John", EmployeDepartment = office });
        Employes.Add(new Employe { EmployeId = 2, EmployeName = "Sue", EmployeDepartment = admin });
        Employes.Add(new Employe { EmployeId = 3, EmployeName = "Bill", EmployeDepartment = admin });
        Employes.Add(new Employe { EmployeId = 4, EmployeName = "Linda", EmployeDepartment = purchasing });
        Employes.Add(new Employe { EmployeId = 5, EmployeName = "Tom", EmployeDepartment = purchasing });
        Employes.Add(new Employe { EmployeId = 6, EmployeName = "Mick", EmployeDepartment = purchasing });

At First I Show the List of the Employes (Works Fine)

        <ListBox x:Name="lbEmployes" ItemsSource="{Binding Employes}" DisplayMemberPath="EmployeName" />

Now I like to Present the Detailed View of SelectedItem by using DataTemplate:

        <ContentControl Content="{Binding ElementName=lbEmployes, Path=SelectedItem}" ContentTemplate="{StaticResource EmployesTemplate}" />

DataTemplate:

    <DataTemplate DataType="{x:Type local:Employe}" x:Key="EmployesTemplate">
        <StackPanel>
            <TextBlock Text="{Binding Path=EmployeId}"></TextBlock>
            <TextBox Text="{Binding Path=EmployeName}"></TextBox>
            <ComboBox ItemsSource="{Binding Path=EmployeDepartment, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="Department" DisplayMemberPath="DepartmentName" />
        </StackPanel>
    </DataTemplate>

Id and Name works Fine. How To Fix The Combobox? How to Bind the ItemsSource and SelectedItem?

Full SourceCode: https://gist.github.com/LwServices/a88818e52a612790e3785e308f5ad2ce

1
Shouldn't the itemsource be departments (the list) and not employeedepartment (this single) and maybe the selecteditem be bound to employeedepartment?Kevin Cook

1 Answers

2
votes

The Correct Binding of the Combobox is:

            <ComboBox ItemsSource="{Binding Departments, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainWindow}}}" DisplayMemberPath="DepartmentName" SelectedItem="{Binding Path=EmployeDepartment}" />