[ACCEPTED]-WPF Databinding stackpanel-stackpanel

Accepted answer
Score: 34

Julien's answer is correct for your written 13 description, however, looking at your XAML, it 12 appears you are looking for something like 11 the following:

<DataTemplate x:Key="UserDataTemplate">
        <Image Source="User.png"/>
        <Label HorizontalAlignment="Center" Content="{Binding Path=UserName}" />

<ItemsControl x:Name="UserList" ItemTemplate="{StaticResource UserDataTemplate}" >
            <VirtualizingStackPanel Orientation="Horizontal"/>

You definately need an ItemsControl 10 (or some derivation of) to bind your source 9 to. Then you can change the the orientation 8 by setting it's items panel (which I believe 7 is a VirtualizingStackPanel with Vertical 6 orientation by default) so just set it to 5 a VirtualizingStackPanel with Horizontal 4 Orientation. Then you can set the ItemsTemplate 3 for each of your items to the layout you 2 desire (an image stacked on top of text 1 bound from your database).

Score: 29

Basically, you want to use a control capable 10 of displaying an enumeration of objects. The 9 control capable of this is the class ItemsControl and 8 all of its descendants (Selector, ListBox, ListView, etc).

Bind 7 the ItemsSource property of this control to a list 6 of objects you want, here a list of users 5 you've fetched from the database. Set the 4 ItemTemplate of the control to a DataTemplate that will be used 3 to display each item in the list.

Sample 2 code:

In a Resources section (for example Window.Resources):

<DataTemplate x:Key="UserDataTemplate">
  <StackPanel Orientation="Horizontal">
    <Image Source="User.png"/>
    <Label HorizontalAlignment="Center" Content="{Binding Path=UserName}" />

In your 1 Window/Page/UserControl:

<ItemsControl x:Name="UserList" ItemTemplate="{StaticResource UserDataTemplate}" />

In your code behind:

UserList.ItemsSource = ... // here, an enumeration of your Users, fetched from your db

More Related questions