I have created a custom user control in my WPF application and a class that has all the properties that correspond to what I want the control to show. I am getting a collection of said class objects from a database and wish to show one custom control per object in a stackpanel-esque display.
I am aware I can get an ItemsControl
, bind the collection to its items source and define a data template. What I'm NOT aware of is how to define a data template that uses my custom control and correctly binds its labels' content to the properties of the corresponding item in the collection.
Basically I have dis:
<ItemsControl ItemsSource="{Binding ObjectsHashSet}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<customControls:myCustomControl/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
For simplycity's sake, let's say this is myCustomControl
:
<UserControl [bla-bla]>
<Label Name="lblName" Content="{Binding Name}"/>
</UserControl>
and it just shows a control with empty label. I would like to know how to tell it to bind the label's content to the "Name" property of the corresponding object in the collection for which it was created.