C# WPF 数据绑定堆栈面板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1525464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Databinding stackpanel
提问by
Im a beginner in WPF programming, coming from .NET 2.0 C#.
我是 WPF 编程的初学者,来自 .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.
我试图制作一个水平的StackPanel
,应该用数据库中表中的数据填充。问题是我希望它显示带有下表中一些文本的图像,然后水平堆叠这两个项目。
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.
我根本无法弄清楚如何做到这一点。
回答by Julien Lebosquain
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).
基本上,您希望使用能够显示对象枚举的控件。能够这样做的控制是类ItemsControl
和其所有后代(的Selector
,ListBox
,ListView
等等)。
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 the ItemTemplate
of the control to a DataTemplate
that will be used to display each item in the list.
ItemsSource
将此控件的属性绑定到您想要的对象列表,这里是您从数据库中获取的用户列表。将ItemTemplate
控件的设置为DataTemplate
将用于显示列表中每个项目的 。
Sample code:
示例代码:
In a Resources
section (for example Window.Resources
):
在一个Resources
部分(例如Window.Resources
):
<DataTemplate x:Key="UserDataTemplate">
<StackPanel Orientation="Horizontal">
<Image Source="User.png"/>
<Label HorizontalAlignment="Center" Content="{Binding Path=UserName}" />
</StackPanel>
</DataTemplate>
In your Window
/Page
/UserControl
:
在你的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
回答by Scott
Julien's answer is correct for your written description, however, looking at your XAML, it appears you are looking for something like the following:
Julien 的回答对于您的书面描述是正确的,但是,查看您的 XAML,您似乎正在寻找以下内容:
<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>
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 (或一些派生)来绑定您的源。然后你可以通过设置它的项目面板来改变方向(我认为默认情况下它是一个具有垂直方向的 VirtualizingStackPanel)所以只需将它设置为一个具有水平方向的 VirtualizingStackPanel。然后,您可以将每个项目的 ItemsTemplate 设置为您想要的布局(堆叠在从数据库绑定的文本顶部的图像)。