Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在XAML中对齐ListView或ItemsControl中的每个n元素_C#_.net_Wpf_Xaml_Data Binding - Fatal编程技术网

C# 如何在XAML中对齐ListView或ItemsControl中的每个n元素

C# 如何在XAML中对齐ListView或ItemsControl中的每个n元素,c#,.net,wpf,xaml,data-binding,C#,.net,Wpf,Xaml,Data Binding,假设我有一个模型 public class Person { public string Name { get; set; } } 在我的视图模型中,我有以下列表: ObservableCollection<Person> People; observedcollection人员; 通常,绑定语法如下所示: <ItemsControl ItemsSource="{Binding People}"> <ItemsControl.ItemTempl

假设我有一个模型

public class Person
{
    public string Name { get; set; }
}
在我的视图模型中,我有以下列表:

ObservableCollection<Person> People;
observedcollection人员;
通常,绑定语法如下所示:

<ItemsControl ItemsSource="{Binding People}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding Name}"/>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
<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>

我想要达到的是每排有3个人。 让我们假设人={A,B,C,D,E,F,G} 我想将其显示为:
A B C
def
G


实现这一点的正确方法是什么?

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

<ItemsControl ItemsSource="{Binding People}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding Name}"/>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
<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>

您可以使用UniformGrid作为项目span如下所示:

<ItemsControl ItemsSource="{Binding People}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding Name}"/>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
<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>