WPF 数据绑定堆栈面板

发布于 2024-08-07 01:16:24 字数 483 浏览 5 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阳光①夏 2024-08-14 01:16:24

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>

您肯定需要一个 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:

<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).

叫思念不要吵 2024-08-14 01:16:24

基本上,您希望使用能够显示对象枚举的控件。能够实现此功能的控件是类 ItemsControl 及其所有后代(SelectorListBoxListView 等) )。

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

示例代码:

Resources 部分(例如 Window.Resources):

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

在您的 Window/Page/< code>UserControl:

<ItemsControl x:Name="UserList" ItemTemplate="{StaticResource UserDataTemplate}" />

在您的代码后面:

UserList.ItemsSource = ... // here, an enumeration of your Users, fetched from your db

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 the ItemTemplate of the control to a DataTemplate that will be used to display each item in the list.

Sample code:

In a Resources section (for example 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:

<ItemsControl x:Name="UserList" ItemTemplate="{StaticResource UserDataTemplate}" />

In your code behind:

UserList.ItemsSource = ... // here, an enumeration of your Users, fetched from your db
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文