Victor Rodrigues
Victor Rodrigues

Reputation: 11713

How to bind a ListBox item to a user control?

People use frequently something like:

<ListBox ItemsSource="{Binding ElementName=thisControl, Path=ListIndexes}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Label Content="{Binding Path=IndexName}"/>
<Label Content="{Binding Path=IndexValue}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

But I would like to use, instead of labels, a control, like this:

<ListBox ItemsSource="{Binding ElementName=thisControl, Path=ListIndexes}">
<ListBox.ItemTemplate>
<DataTemplate>
<local:Index Item="**{Binding}**"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

My doubt is what to put into this Binding to include the whole item from the collection.

Upvotes: 8

Views: 5388

Answers (1)

Ray Booysen
Ray Booysen

Reputation: 29991

The syntax for this is:

<local:Index Item="{Binding}"/>

This will tell the data binding functions to bind the entire datacontext for each ListBox Item to the Item property in your Index control

Upvotes: 15

Related Questions