Peter Majeed
Peter Majeed

Reputation: 5362

How do you set the DisplayMemberPath for a List of Lists (at runtime)?

I have the below classes (abbreviated for simplicity):

namespace Test
{
    class Class1
    {
        Class2 Property2 { get; set; }
        Class3 Property3 { get; set; }
    }

    class Class2
    {
        string ColumnName { get; set; }
    }

    class Class3
    {
        string ColumnName { get; set; }
        bool IsRequired { get; set; }
    }
}

I instantiate a list of Class1 and populate it, then set the databinding of a ListView to my list as below.

List<Class1> pList = ...;
listView1.ItemsSource = pList;

I've tried to set listView1.DisplayMemberPath equal to "ColumnName", "Class2.ColumnName", "Class3.ColumnName", "Test.Class2.ColumnName", but nothing seems to work (the ListView displays blank items. If I set the DisplayMemberPath to a list of Class2 or Class3, the ListView displays, but I'd like to keep a list of Class1 as my datasource.

Is there anything simple I'm missing here (I'm assuming this is very possible)? (I've searched far and wide for a solution to my problem (which is hopefully really simple), but I haven't gotten anything to work yet.)

Upvotes: 0

Views: 698

Answers (2)

Steve
Steve

Reputation: 216313

The name of your internal classes are Property2 and Property3,
so I think that the right use for listView1.DisplayMemberPath is:

listView1.DisplayMemberPath = "Property2.ColumnName"; 

Upvotes: 2

dowhilefor
dowhilefor

Reputation: 11051

DisplayMemberPath is just a shortcut. You can always use the more flexible way of providing a DataTemplate.

<DataTemplate x:Key="myTemplate">
    <TextBlock Text="{Binding Property2.ColumnName}"/>
</DataTemplate>

<ItemsControl ItemTemplate="{StaticResource myTemplate}"/>

Upvotes: 2

Related Questions