WPF:ItemsControl中的行和列 [英] WPF: rows and columns in an ItemsControl

查看:592
本文介绍了WPF:ItemsControl中的行和列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过将RowDefinitionsColumnDefinitions设置为ItemsPanel属性的网格来将ListViewItemsControl的子级放在行和列中.

I've tried putting children of both a ListView and an ItemsControl in rows and columns, by setting a grid with RowDefinitions and ColumnDefinitions as the ItemsPanel property.

但是当我放置时,子控件始终与第1行和第1列对齐

However the child control always aligns to Row 1 and Column 1, when I put

<ItemsControl>

   <ItemsControl.ItemsPanel>
      <!-- Grid with rows & columns ... -->
   </ItemsControl.ItemsPanel>

   <ItemsControl.ItemTemplate>
      <DataTemplate>
         <TextBlock Grid.Row="4" Grid.Column="2" ... />
      </DataTemplate>
   </ItemsControl.ItemTemplate>

</ItemsControl>

我该如何进行这项工作?谢谢.

How can I make this work? Thank you.

推荐答案

在"ItemTemplate"(而不是"ItemPanel")中设置"Grid".在此处查看示例: http://www.wpf-tutorial.com/list-controls /itemscontrol/

Set the ´Grid´ in the ´ItemTemplate´ rather than the ´ItemPanel´. See example here: http://www.wpf-tutorial.com/list-controls/itemscontrol/

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        PersonCollection = new ObservableCollection<Person>()
        {
            new Person() { FirstName = "John", LastName = "Doe" },
            new Person() { FirstName = "Richard", LastName = "Bryson" },
            new Person() { FirstName = "Bill", LastName = "Gates" },
            new Person() { FirstName = "Adam", LastName = "Sandler" }
        };
        itemsControl.ItemsSource = PersonCollection;
    }
    public ObservableCollection<Person> PersonCollection { get; set; }
}


<ItemsControl x:Name="itemsControl">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid Margin="0,0,0,5">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>
                <TextBlock Grid.Column="0" Text="{Binding Path=FirstName}" />
                <TextBlock Grid.Column="1" Text="{Binding Path=LastName}" />
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

这篇关于WPF:ItemsControl中的行和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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