Hello. Faced a problem. I make an application on WPF using the MVVM pattern. I work with the database using the Entity Framework. There are 2 tables: Employees (name, position id) and Posts (id, title). I want the form with the staff, when I choose one of them, to display all the information about it (name, position, etc.). I implemented this by creating the EmployeViewModel class:
class EmployeesViewModel : INotifyPropertyChanged { OpticsEntities db; private Employee selectedEmployees; public ObservableCollection<Employee> Employees { get; set; } public Employee SelectedEmployees { get { return selectedEmployees; } set { selectedEmployees = value; OnPropertyChanged("SelectedEmployees"); } } public EmployeesViewModel() { db = new OpticsEntities(); db.Employees.Load(); Employees = db.Employees.Local; } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged([CallerMemberName]string prop = "") { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(prop)); } private RelayCommand addCommand; private RelayCommand saveCommand; private RelayCommand removeCommand; private RelayCommand changeCommand; public RelayCommand AddCommand { get { return addCommand ?? (addCommand = new RelayCommand(obj => { Employee employees = new Employee(); Employees.Insert(0, employees); SelectedEmployees = employees; })); } } public RelayCommand SaveCommand { get { return saveCommand ?? (saveCommand = new RelayCommand(obj => { Employee employees = obj as Employee; if (employees != null) { db.Employees.Add(employees); db.SaveChanges(); } })); } } public RelayCommand ChangeCommand { get { return changeCommand ?? (changeCommand = new RelayCommand(obj => { Employee employees = obj as Employee; if (employees != null) { var changeEmployees = db.Employees.Find(employees.id); employees = changeEmployees; db.SaveChanges(); } })); } } } and presentation:
<ListBox Grid.Column="0" ItemsSource="{Binding Employees}" SelectedItem="{Binding SelectedEmployees}" Margin="0,0,17,0"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Margin="5"> <TextBlock FontSize="18" Text="{Binding Path=lastname}" /> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> <StackPanel Grid.Row="1" Orientation="Horizontal"> <Button Command="{Binding AddCommand}">Add</Button> <Button Command="{Binding SaveCommand}" CommandParameter="{Binding SelectedEmployees}">Save</Button> </StackPanel> <StackPanel Grid.Column="1" DataContext="{Binding SelectedEmployees}"> <TextBlock FontWeight="Bold" Text="Имя" /> <TextBox> <TextBox.Text> <Binding Path="firstname" NotifyOnValidationError="True"> <Binding.ValidationRules> <DataErrorValidationRule /> </Binding.ValidationRules> </Binding> </TextBox.Text> </TextBox> <TextBlock FontWeight="Bold" Text="Фамилия" /> <TextBox> <TextBox.Text> <Binding Path="lastname" NotifyOnValidationError="True"> <Binding.ValidationRules> <DataErrorValidationRule /> </Binding.ValidationRules> </Binding> </TextBox.Text> </TextBox> <TextBlock FontWeight="Bold" Text="Должность" /> <TextBox> <TextBox.Text> <Binding Path="post" NotifyOnValidationError="True"> <Binding.ValidationRules> <DataErrorValidationRule /> </Binding.ValidationRules> </Binding> </TextBox.Text> </TextBox> </StackPanel> Everything works fine. You can add a new employee and save. 
But the problem is this. The field "Position" is connected with the table "Positions" by the foreign key (id). And I want, instead of the text field with the post id, a ComboBox, in which the names of all posts from the "Posts" table would be found. By default, it would be the position to which the employee corresponds, but you could open the ComboBox and assign it to another. After clicking on Save, everything would be saved in the database.
The most I could do was create a ComboBox that would display these posts. But for this, I had to make another collection in the ViewModel and access it in ItemsSource. Naturally, whatever I chose in it, there will be no connection with the employee, since the data is not connected.
How can I make the ComboBox display positions from the Positions table, and if one of them was selected, it would be assigned to the Position field of the Employee? Let me remind you that the tables are connected by a foreign key according to the position id.