WPF 数据绑定堆栈面板 [英] WPF Databinding stackpanel

查看:11
本文介绍了WPF 数据绑定堆栈面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 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>

我根本不知道如何做到这一点.

I simply cannot figure oout how to do this.

推荐答案

Julien 的回答对于您的书面描述是正确的,但是,查看您的 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).

这篇关于WPF 数据绑定堆栈面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆