Hi,
you can add a list of DynamicProperty to yor Person class and show items like in following demo:
XAML:
<Window x:Class="WpfApp1.Window065"
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:WpfApp065"
mc:Ignorable="d"
Title="Demo 15709958 Windows-WPF" Height="450" Width="800">
<Window.Resources>
<local:ViewModel x:Key="vm"/>
</Window.Resources>
<Grid DataContext="{StaticResource vm}">
<ItemsControl x:Name="DynamicPeople" ItemsSource="{Binding Path=People}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Background="Gray" Margin="6">
<StackPanel Orientation="Horizontal">
<Label Content="Id"/>
<Label Content="{Binding Path=Id}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="Name"/>
<Label Content="{Binding Path=Name}"/>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="Age"/>
<Label Content="{Binding Path=Age}"/>
</StackPanel>
<ItemsControl ItemsSource="{Binding DynamicProperties}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Background="Gray" Orientation="Horizontal">
<Label Content="{Binding Name}"/>
<Label Content="{Binding Value}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<Button Width="120" Height="60"
Command="{Binding Source={StaticResource vm}}"
CommandParameter="{Binding}">Add New Property</Button>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</Window>
And code:
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
namespace WpfApp065
{
public class ViewModel : ICommand
{
public ViewModel()
{
ObservableCollection<Person> col = new ObservableCollection<Person>();
for (int i = 1; i < 10; i++) col.Add(new Person() { Id = i, Name = $"Name {i}", Age = 20 + i * 3 });
cvs.Source = col;
}
CollectionViewSource cvs = new CollectionViewSource();
public ICollectionView People { get => cvs.View; }
int nr;
public void Execute(object parameter)
{
Person p = parameter as Person;
p.DynamicProperties.Add(new DynamicProperty() { Name = $"Prop {++nr}", Type = typeof(string), Value = nr + 10 });
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter) => true;
}
public class Person
{
public int Id { set; get; }
public string Name { set; get; }
public int Age { set; get; }
public ObservableCollection<DynamicProperty> DynamicProperties { get; } = new ObservableCollection<DynamicProperty>();
}
public class DynamicProperty
{
public string Name { set; get; }
public Type Type { set; get; }
public object Value { set; get; }
}
}
Result: