In short: I have a ListView and when I select an Item of the ListView, this item should show up and edited in a detail UserControl.
I have a Window (ViewMain) with a UserControl (UserControlEmployees) that has a ListView and another UserControl (UserControlEmployeeDetails). The ListView's items are displayed by a third UserControl (UserControlEmployee). UserControlEmployees has two dependency properties: a ObservableCollection (Employees) and a single Employee (SelectedEmployee). The ViewModel passes a ObservableCollection to UserControlEmployees. UserControlEmployees then passes the Employees to the ListView. The ListView's SelectedItem is bound to SelectedEmployee.
SelectedEmployee is supposed to also be bound to UserControlEmployeeDetails. So I tried to bind ViewModelEmployeeDetail and the SelectedItem of the ListView to the same dependency property.
I guess the prtoblem is in UserControlEmployees: My idea was that control.ControlEmployeesListView.SelectedItem = e.NewValue as Employee; would bind SelectedItem to SelectedEmployee. But this is not working and I have no idea how else I can bind it. Usually I would do somthing like in XAML, but I don't have access to that in this case.
EDIT I noticed that I have forgotten to set my ListView SelectedItem to Binding.
<ListView
x:Name="ControlEmployeesListView"
Grid.Row="0"
SelectedItem="{Binding Mode=TwoWay}">
I fixed that but now I get this exception:
System.Windows.Markup.XamlParseException: ''Provide value on 'System.Windows.Data.Binding' threw an exception.' Line number '26' and line position '17'.'
Inner Exception InvalidOperationException: Two-way binding requires Path or XPath.
/EDIT UserControlEmployees.xaml
<UserControl
x:Class="TestNestedUserControls.View.UserControls.UserControlEmployees"
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:uc="clr-namespace:TestNestedUserControls.View.UserControls"
d:DesignHeight="25"
d:DesignWidth="300"
mc:Ignorable="d">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!-- ListView -->
<ListView Grid.Row="0">
<ListView x:Name="ControlEmployeesListView" Grid.Row="0">
<ListView.ItemTemplate>
<DataTemplate>
<uc:UserControlEmployeeListItem EmployeeListItem="{Binding}" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ListView>
<!-- Details -->
<uc:UserControlEmployeeDetails x:Name="ControlUserControlEmployeeDetails" Grid.Row="1" />
<!-- SelectedEmployee="{Binding}" -->
</Grid>
</UserControl>
Thats the code in its UserControlEmployees.xaml.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TestNestedUserControls.Model;
namespace TestNestedUserControls.View.UserControls
{
/// <summary>
/// Interaction logic for UserControlEmployees.xaml
/// </summary>
public partial class UserControlEmployees : UserControl, INotifyPropertyChanged
{
public UserControlEmployees()
{
InitializeComponent();
}
// List Items
public ObservableCollection<Employee> Employees
{
get { return (ObservableCollection<Employee>)GetValue(EmployeesProperty); }
set
{
SetValue(EmployeesProperty, value);
NotifyPropertyChanged();
}
}
public static readonly DependencyProperty EmployeesProperty =
DependencyProperty.Register(nameof(Employees), typeof(ObservableCollection<Employee>), typeof(UserControlEmployees), new PropertyMetadata(default, SetNew));
private static void SetNew(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = d as UserControlEmployees;
if (control != null)
{
control.ControlEmployeesListView.ItemsSource = e.NewValue as ObservableCollection<Employee>;
}
}
//Selected Item
public Employee SelectedEmployee
{
get { return (Employee)GetValue(EmployeeProperty); }
set
{
SetValue(EmployeeProperty, value);
NotifyPropertyChanged();
}
}
public static readonly DependencyProperty EmployeeProperty =
DependencyProperty.Register(nameof(SelectedEmployee), typeof(Employee), typeof(UserControlEmployees), new PropertyMetadata(default, SetNewSelected));
private static void SetNewSelected(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = d as UserControlEmployees;
if (control != null)
{
control.ControlUserControlEmployeeDetails.EmployeeDetail = e.NewValue as Employee;
control.ControlEmployeesListView.SelectedItem = e.NewValue as Employee;
}
}
#region INotifyPropertyChanged ⬇️
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion ⬆️
}
}