Skip to content

Instantly share code, notes, and snippets.

@LwServices
Created February 2, 2018 12:18
Show Gist options
  • Save LwServices/a88818e52a612790e3785e308f5ad2ce to your computer and use it in GitHub Desktop.
Save LwServices/a88818e52a612790e3785e308f5ad2ce to your computer and use it in GitHub Desktop.
public class Employe : INotifyPropertyChanged
{
    public int EmployeId { get; set; }
    public string EmployeName { get; set; }
    public Department EmployeDepartment { get; set; }
    //public override string ToString()
    //{
    //    return $"{EmployeName} ";
    //}
    public event PropertyChangedEventHandler PropertyChanged;
}
public partial class MainWindow : Window
  {
      public List<Department> Departments { get; set; } = new List<Department>();
      public List<Employe> Employes { get; set; } = new List<Employe>();
      public MainWindow()
      {
          InitializeComponent();
          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 });
      }
  }
public class Department : INotifyPropertyChanged
{
    public int DepartmentId { get; set; }
    public string DepartmentName { get; set; }
    public event PropertyChangedEventHandler PropertyChanged;
}
<Window x:Class="WpfBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfBinding"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <DataTemplate DataType="{x:Type local:Employe}" x:Key="EmployesTemplate">
            <StackPanel>
                <Label Content="Id" />
                <TextBlock Text="{Binding Path=EmployeId}"></TextBlock>
                <Label Content="Name" />
                <TextBox Text="{Binding Path=EmployeName}"></TextBox>
                <Label Content="Department" />
                <ComboBox ItemsSource="{Binding Path=EmployeDepartment, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="Department" DisplayMemberPath="DepartmentName" />
            </StackPanel>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="1*" />
        </Grid.ColumnDefinitions>
        <StackPanel Grid.Column="0">
            <ListBox x:Name="lbEmployes" ItemsSource="{Binding Employes}" DisplayMemberPath="EmployeName" />
        </StackPanel>
        <StackPanel Grid.Column="1">
            <ContentControl Content="{Binding ElementName=lbEmployes, Path=SelectedItem}" ContentTemplate="{StaticResource EmployesTemplate}" />
        </StackPanel>
        <StackPanel Grid.Column="2">
            <Label Content="Id" />
            <TextBlock Text="{Binding ElementName=lbEmployes, Path=SelectedItem}"></TextBlock>
            <Label Content="Name" />
            <TextBox Text="{Binding ElementName=lbEmployes, Path=SelectedItem}"></TextBox>
            <Label Content="Department" />
            <ComboBox ItemsSource="{Binding Departments, Mode=OneWay}" SelectedValuePath="Department" DisplayMemberPath="DepartmentName" />
        </StackPanel>
    </Grid>
</Window>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment