Hi Pablo,
try following demo:
XAML MainWindow:
<Window x:Class="WpfApp1.Window006"
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:WpfApp006"
mc:Ignorable="d"
Title="Master - Detail with Detail List" Height="450" Width="800">
<Window.Resources>
<local:ViewModel x:Key="vm"/>
</Window.Resources>
<Grid DataContext="{StaticResource vm}">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<DataGrid ItemsSource="{Binding View}" IsReadOnly="True" AutoGenerateColumns="false">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}"/>
<DataGridTemplateColumn Header="Telefones">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<ComboBox ItemsSource="{Binding TelefonNumbers}"
DisplayMemberPath="Number"/>
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
<Grid Grid.Column="1" DataContext="{Binding Detail}">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="ID: " HorizontalAlignment="Right"/>
<TextBox Grid.Row="0" Grid.Column="1" Text="{Binding ID}" IsReadOnly="True"/>
<Label Grid.Row="1" Grid.Column="0" Content="Info: " HorizontalAlignment="Right"/>
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Name}"/>
<Label Grid.Row="2" Grid.Column="0" Content="Telefon numbers"/>
<DataGrid Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" ItemsSource="{Binding TelefonNumbers}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Date" Binding="{Binding Date}"/>
<DataGridTextColumn Header="Telefon" Binding="{Binding Number}"/>
<DataGridTemplateColumn Header="Delete">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Delete" Command="{Binding Cmd, Source={StaticResource vm}}" CommandParameter="{Binding}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Grid>
</Window>
And classes:
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
namespace WpfApp006
{
public class ViewModel : INotifyPropertyChanged
{
private CollectionViewSource cvs = new CollectionViewSource();
private ObservableCollection<PersonData> col = new ObservableCollection<PersonData>();
public ICollectionView View
{
get
{
if (cvs.Source == null)
{
GetData();
cvs.View.CurrentChanged += (sender, e) => Detail = cvs.View.CurrentItem as PersonData;
}
return cvs.View;
}
}
private PersonData _detail = null;
public PersonData Detail { get => this._detail; set { this._detail = value; OnPropertyChanged(); } }
private void GetData()
{
Random rnd = new Random();
for (int i = 1; i < 11; i++)
{
PersonData p = new PersonData { ID = i, Name = $"Name {i}" };
col.Add(p);
for (int k = 0; k < rnd.Next(3, 9); k++)
p.TelefonNumbers.Add(new TelefonNumber() { Date = DateTime.Now.AddDays(rnd.Next(-1000, 1000)).AddMinutes(rnd.Next(0, 50000)), Number = rnd.Next(10000, 100000).ToString() });
}
cvs.Source = col;
}
public ICommand Cmd
{
get => new RelayCommand((state) =>
{
TelefonNumber t = state as TelefonNumber;
if (t != null) Detail.TelefonNumbers.Remove(t);
}, null);
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public class PersonData
{
public int ID { get; set; }
public string Name { get; set; }
public ObservableCollection<TelefonNumber> TelefonNumbers { get; set; } = new ObservableCollection<TelefonNumber>();
}
public class TelefonNumber
{
public DateTime Date { get; set; }
public string Number { get; set; }
}
public class RelayCommand : ICommand
{
private readonly Predicate<object> _canExecute;
private readonly Action<object> _action;
public RelayCommand(Action<object> action, Predicate<object> canExecute) { _action = action; _canExecute = canExecute; }
public void Execute(object o) => _action(o);
public bool CanExecute(object o) => _canExecute == null ? true : _canExecute(o);
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
}
}
Result: