Bind nested object to data grid

١٥٥ и 21 Reputation points

I have a DataGrid that has two columns (A and B). Each column cell contains a combo box control.

I have an object that contains two list items that I want to bind to the grid view, but the binding does not work.

Binding in both DataTemplates not working correctly. UI shows the namespace instead of value.

Sample Project:!Ah27QQNpKj4jj2xsIsQamaOXOlin?e=jNAj2H

View Model:

public class MainWindowViewModel {
  public List < MainObject > MainObjects { get; set; }

  public MainWindowViewModel() {
    MainObjects = new List < MainObject > {
      new MainObject {
        AColumns = new List < ColumnA > {
          new ColumnA { Name = "A1" },
          new ColumnA { Name = "A1" }
        BColumns = new List < ColumnB > {
          new ColumnB { Name = "B1" },
          new ColumnB { Name = "B1" }

public class MainObject {
  public List < ColumnA > ?AColumns { get; set; }
  public List < ColumnB > ?BColumns { get; set; }

public class ColumnA {
  public string ? Name { get; set; }


<Window x:Class="TestDataGrid.MainWindow"
        Title="MainWindow" Height="450" Width="800">
    <DataGrid ItemsSource="{Binding MainObjects}" AutoGenerateColumns="False">
            <DataGridTemplateColumn Header="Column A" Width="Auto">
                        <TextBlock HorizontalAlignment="Center">
                                    <Binding Path="Name" Mode="TwoWay" />
                        <ComboBox IsEditable="True" Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding AColumns}"/>
Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,685 questions
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
768 questions
{count} votes

Accepted answer
  1. Hui Liu-MSFT 40,866 Reputation points Microsoft Vendor

    You could modify the binding of the Combobox as follows:

                            <ComboBox IsEditable="True" DisplayMemberPath="Name"  ItemsSource="{Binding AColumns}"/>  

    The 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. 


    0 comments No comments

0 additional answers

Sort by: Most helpful