简体   繁体   中英

WPF: Binding the items in a “sub-list” of a list

I have a Datagrid in WPF that I bind to my ObservableCollection List, then I create the Columns for properties that I want, like the following ones:

    public int Name { get; set; }
    public DateTime Date { get; set; }
    public long Cod { get; set; }
    public long ProductRef{ get; set; }

    public List<SubProduct> ListSpecs {get; set;}

The problem is, that collection has a list of sub-products and I need to create a new column for at least the first Item of that list. The Datagrid would be like the following:

|Name|Date|Cod|ProductRef|ListSpecs[0]|ListSpecs[1]|

How can that be achieved? I don't want to add more properties to bind the items of that list.

The following should work. For example for your first item of the subcollection. Or you could try out binding the entire subcollection to a combobox?

        <DataGrid.Columns>
            <DataGridTextColumn Header="item" Binding="{Binding Name}"  />
            <DataGridTextColumn Header="subitems" Binding="{Binding ListSpecs[0]}"  />  // first item of subcollection
            <DataGridTextColumn Header="subitems" Binding="{Binding ListSpecs[1]}"  /> // second item of sub collection
        </DataGrid.Columns>    
    </DataGrid>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM