简体   繁体   English

WPF:ItemsControl中的行和列

[英]WPF: rows and columns in an ItemsControl

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. 我试图把既有的儿童ListViewItemsControl行和列,通过设置与网格RowDefinitionsColumnDefinitions作为ItemsPanel属性。

However the child control always aligns to Row 1 and Column 1, when I put 但是,当我将子控件放到第1行和第1列时,

<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. 谢谢。

You should set the Grid.Row and Grid.Column attached properties of the container that wraps the root element of the ItemTemplate : 您应该设置包装了ItemTemplate根元素的容器的Grid.RowGrid.Column附加属性:

<ItemsControl x:Name="iccc">

    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
            </Grid>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>

    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock>cell 2:2...</TextBlock>
        </DataTemplate>
    </ItemsControl.ItemTemplate>

    <ItemsControl.ItemContainerStyle>
        <Style TargetType="ContentPresenter">
            <Setter Property="Grid.Row" Value="1" />
            <Setter Property="Grid.Column" Value="1" />
        </Style>
    </ItemsControl.ItemContainerStyle>

</ItemsControl>

Set the ´Grid´ in the ´ItemTemplate´ rather than the ´ItemPanel´. 在“ ItemTemplate”而不是“ ItemPanel”中设置“ Grid”。 See example here: http://www.wpf-tutorial.com/list-controls/itemscontrol/ 请参阅此处的示例: 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM