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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:25:29  来源:igfitidea点击:

WPF Databinding stackpanel

c#wpfdata-bindingstackpanel

提问by

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

我是 WPF 编程的初学者,来自 .NET 2.0 C#。

Im trying to make a horizontal StackPanelwhich 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 ItemsControland all of its descendants (Selector, ListBox, ListView, etc).

基本上,您希望使用能够显示对象枚举的控件。能够这样做的控制是类ItemsControl和其所有后代(的SelectorListBoxListView等等)。

Bind the ItemsSourceproperty of this control to a list of objects you want, here a list of users you've fetched from the database. Set the ItemTemplateof the control to a DataTemplatethat will be used to display each item in the list.

ItemsSource将此控件的属性绑定到您想要的对象列表,这里是您从数据库中获取的用户列表。将ItemTemplate控件的设置为DataTemplate将用于显示列表中每个项目的 。

Sample code:

示例代码:

In a Resourcessection (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 设置为您想要的布局(堆叠在从数据库绑定的文本顶部的图像)。