byte
byte

Reputation: 1685

Wpf binding with nested properties

ViewModel

I have a property of type Member called KeyMember. The 'Member' type has an ObservableCollection called Addresses. The Address is composed of two strings - street and postcode .

View

I have a ListBox whose item source need to be set to ViewModels's KeyMember property and it should display the Street of all the Past Addresses in the Address collection.

Question

My ViewModel and View relationship is established properly.

I am able to write a data template for the above simple case as below

<ListBox ItemsSource="{Binding KeyMember.Addresses}">
    <ListBox.ItemTemplate>
        <DataTemplate DataType="Address">
            <TextBlock Text="{Binding Street}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

I am curios to know how to write the DataTemplate if I change KeyMember from type Member to ObservableCollection< Member > assuming that the collection has only one element. I am not sure whether this is a valid scenrio and it's implementation feasibility.

PS: I know that for multiple elements in collection, I will have to implement the Master-Detail pattern/scenario. I am looking into this at the moment.

Upvotes: 1

Views: 1196

Answers (1)

JustABill
JustABill

Reputation: 1559

If you want to bind to the 0th element, you can do {Binding Path=[0].Addresses}, and likewise for any other elements in a collection that supports array-style indexing. I agree with Wonko though that this is a rather unusual requirement.

Upvotes: 2

Related Questions