WPF 数据绑定堆栈面板
我是 WPF 编程的初学者,来自 .NET 2.0 C#。
我正在尝试制作一个水平的 StackPanel,它应该填充数据库中表中的数据。问题是我希望它显示带有下表中的一些文本的图像,然后水平堆叠这两个项目。
这是一些伪代码来显示我想要做的事情:
<StackPanel Orientation="horizontal" ItemsSource="{Binding Path=myTable}">
<StackPanel>
<Image Source="User.png"/>
<Label HorizontalAlignment="Center" Content="{Binding Path=UserName}"></Label>
</StackPanel>
</StackPanel>
我根本不知道如何做到这一点。
Im a beginner in WPF programming, coming from .NET 2.0 C#.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Julien 的答案对于您的书面描述来说是正确的,但是,查看您的 XAML,您似乎正在寻找类似以下内容的内容:
您肯定需要一个 ItemsControl (或其某些派生)来将您的源绑定到。然后,您可以通过设置其项目面板(我认为默认情况下是具有垂直方向的 VirtualizingStackPanel)来更改方向,因此只需将其设置为具有水平方向的 VirtualizingStackPanel 即可。然后,您可以将每个项目的 ItemsTemplate 设置为您想要的布局(堆叠在从数据库绑定的文本顶部的图像)。
Julien's answer is correct for your written description, however, looking at your XAML, it appears you are looking for something like the following:
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).
基本上,您希望使用能够显示对象枚举的控件。能够实现此功能的控件是类
ItemsControl
及其所有后代(Selector
、ListBox
、ListView
等) )。将此控件的
ItemsSource
属性绑定到所需的对象列表,此处是您从数据库获取的用户列表。将控件的ItemTemplate
设置为将用于显示列表中每个项目的DataTemplate
。示例代码:
在
Resources
部分(例如Window.Resources
):在您的
Window
/Page
/< code>UserControl:在您的代码后面:
Basically, you want to use a control capable of displaying an enumeration of objects. The control capable of this is the class
ItemsControl
and all of its descendants (Selector
,ListBox
,ListView
, etc).Bind the
ItemsSource
property of this control to a list of objects you want, here a list of users you've fetched from the database. Set theItemTemplate
of the control to aDataTemplate
that will be used to display each item in the list.Sample code:
In a
Resources
section (for exampleWindow.Resources
):In your
Window
/Page
/UserControl
:In your code behind: