For a one-to-many relationship, we take a collection of one class as an property of another class. The following is an example of binding a DataGrid to a one-to-many relationship model. You could try to refer to it.
<Grid>
<DataGrid ItemsSource="{Binding Source}" Width="800">
<DataGrid.Columns>
<DataGridTemplateColumn Header="main" Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Width="150" Text="{Binding SelectedName}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox Width="190" SelectedValuePath="Name"
SelectedValue="{Binding SelectedName}"
SelectedItem="{Binding SelectedMainItem, UpdateSourceTrigger=PropertyChanged}"
DisplayMemberPath="Name"
ItemsSource="{Binding SomeBinding}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="sub" Width="100">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Width="150" Text="{Binding SelectedSubitem}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox SelectedValue="{Binding SelectedSubitem}"
ItemsSource="{Binding Path=SelectedMainItem.Subitems}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
The code of xaml:
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
namespace WpfApp3
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new VMColl();
}
}
public class VMColl
{
List<VM> source;
public List<VM> Source
{
get { return source; }
set { source = value; }
}
public VMColl()
{
source = new List<VM>() { new VM(), new VM(), new VM(), new VM() };
}
}
public class VM : Notify
{
private List<mainObj> _items;
public List<mainObj> SomeBinding
{
get { return _items; }
set
{
_items = value;
OnPropertyChanged("Items");
}
}
private string _selectedName;
public string SelectedName
{
get { return _selectedName; }
set
{
_selectedName = value;
OnPropertyChanged("SelectedName");
}
}
private mainObj _selectedMainItem;
public mainObj SelectedMainItem
{
get { return _selectedMainItem; }
set
{
_selectedMainItem = value;
OnPropertyChanged("SelectedMainItem");
OnPropertyChanged("SelectedMainItem.Subitems");
}
}
private string _selectedSubitem;
public string SelectedSubitem
{
get { return _selectedSubitem; }
set
{
_selectedSubitem = value;
OnPropertyChanged("SelectedSubitem");
}
}
public VM()
{
SomeBinding = new List<mainObj>() { new mainObj("first"), new mainObj("second"), new mainObj("someother") };
}
}
public class mainObj : Notify
{
private BindingList<string> _subitems;
public BindingList<string> Subitems
{
get { return _subitems; }
set
{
_subitems = value;
OnPropertyChanged("Subitems");
}
}
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged("Name");
}
}
public mainObj(string name)
{
_name = name;
_subitems = new BindingList<string>() { "1", "2", "3" };
}
public override string ToString()
{
return Name;
}
}
public class Notify : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
}
The picture of result:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread.
[5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html