How can I specify datasource for a user control:
<ScrollViewer VerticalScrollBarVisibility="Visible" VerticalAlignment="Stretch">
<ListBox ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<local:ucMyControl />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ScrollViewer>
How ucMyControl know what values to take? I realize that lisbox will create as many ucMyControls as there are items in bondong collection?
Also, scrollviewers don’t work – I can’t get to 3rd control (they are all empty anyway, but I hope you’ll give me leads how to bind it).
The control that is instantiated from an ItemTemplate in an
ItemsControls(e.g.ListBox) has its DataContext property automatically set to the appropriate item from theItemsorItemsSourcecollection of the ItemsControl.So if you for example have a collection of
Personobjects with propertiesFirstNameandLastNameas ListBox items you can bind like this in your UserControl:You do not need to set the DataContext property explicitly.