如何在XAML中对齐ListView或ItemsControl中的每个n元素 [英] How to align every n element in a ListView or ItemsControl in XAML

查看:101
本文介绍了如何在XAML中对齐ListView或ItemsControl中的每个n元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有模型

public class Person
{
    public string Name { get; set; }
}

在我的ViewModle中,我有以下列表:

And in my ViewModle i have this list:

ObservableCollection<Person> People;

通常,绑定语法如下:

<ItemsControl ItemsSource="{Binding People}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding Name}"/>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

我想实现的目标是每行3个人。
因此,假设人= {A,B,C,D,E,F,G}
我想显示为:

ABC

DEF

G

What i want to achive is to have for each row 3 people. So let's say People = {A,B,C,D,E,F,G} I want to display it like:
A B C
D E F
G

实现此目标的正确方法是什么?

What is the proper way to achieve this ?

推荐答案

您可以使用 UniformGrid 作为ItemsPanel,如下所示:

You could Use a UniformGrid as ItemsPanel like this:

<ItemsControl ItemsSource="{Binding People}">
    <ItemsControl.ItemsPanel>
       <ItemsPanelTemplate>
          <UniformGrid Columns="3"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding Name}"/>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

这篇关于如何在XAML中对齐ListView或ItemsControl中的每个n元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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