WPF数据绑定的StackPanel

问题描述:

林在WPF编程初学者,从.NET 2.0 C#来了。

Im a beginner in WPF programming, coming from .NET 2.0 C#.

我试着做一个横向的StackPanel 应在数据库填充数据从一个表。问题是,我希望它与从下表中的一些文字显示图像,然后水平堆叠这两个项目。

Im trying to make a horizontal StackPanel which should be filled with data from a table in a database. The problem is that I want it to display an image with some text from the table below and then stack those two items horizontally.

下面是一些伪代码,以显示我的想做的事:

Here's some pseudo-code to display what I want to do:

<StackPanel Orientation="horizontal" ItemsSource="{Binding Path=myTable}">
    <StackPanel>
        <Image Source="User.png"/>
        <Label HorizontalAlignment="Center" Content="{Binding Path=UserName}"></Label>
    </StackPanel>
</StackPanel>



我根本想不通oout如何做到这一点。

I simply cannot figure oout how to do this.

朱利安的答案是你的书面说明正确的,不过,看你的XAML,看来你正在寻找类似如下:

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

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

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

您肯定需要一个ItemsControl(或一些推导)到源绑定。然后,你可以通过设置它的项目小组(我相信这是一个VirtualizingStackPanel与垂直方向默认情况下),所以只是将其设置为VirtualizingStackPanel与水平方向改变的方向。然后,你可以为每个项目的ItemsTemplate设置为你的愿望(堆放在从数据库绑定文本之上的图像)的布局。

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