0

I have this view model bound to the DataContext of a UserControl:

public SomeViewModel : BaseViewModel
{
    public IEnumerable<Products> Products { get; set; }
    ...
}

public class Products
{
    public string Ident { get; set; }
    public string Name { get; set; }
    public ICollection<Provider> Providers { get; set; }
}

public class Provider
{
    public string Name { get; set; }
    public string Address { get; set; }
}

I want to bind the SomeViewModel.Products property to a list view so that the first two columns are the Ident and Name of the product (and then there is a set of columns for every Provider) like this:

example listview

I need the listview to have a dynamic number of columns depending on the number of items in the collection. If the collection has 2 Providers, the listview should have 6 columns (the first two which are bound to the Ident and Name properties of the Product, and 2 additional columns to the first Provider and another 2 columns to the second Provider).

This XAML code works to bind the first two columns to the Ident and Name properties of the Products object (the ListView ItemsSource is bound to the Products object in code-behind):

<ListView>
    <ListView.View>
        <GridView AllowsColumnReorder="true" ColumnHeaderToolTip="un tooltip pero de todos los headers de columnas joder...">
            <GridViewColumn DisplayMemberBinding="{ Binding Ident }" Header="Ident" Width="50" />
            <GridViewColumn DisplayMemberBinding="{ Binding Name }" Header="Name" Width="200" />

        </GridView>
    </ListView.View>
</ListView>

But how can I make the listview have aditional columns bound to properties in each of the items in the collection (to show Name and Address of each Provider)?

4
  • Use GridViewColumns for your desired columns within the ListView.View property in XAML. Then set the DisplayMemberBinding property to your desired model properties. Seems like a duplicate of link Commented Oct 8 at 16:47
  • Implement as a "two" data grid/LV parent-child view. Either side by side; or one above the other. The selection changed event can be used to sync one grid to another (filter the second items' source). Commented Oct 9 at 1:36
  • Create as many columns as necessary and set their DisplayMemberBinding in code behind. Use indexers in the Bindings' PropertyPath. Multiple ListViews don't make sense. Commented Oct 9 at 5:40
  • @Clemens Shouldn't revision #5 have been part of an answer rather than an edit to the question? Commented Oct 9 at 17:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.