WPF:ItemsControl 中的行和列

Fab*_*abi 2 c# wpf xaml itemscontrol

我已经尝试通过设置一个带有和作为属性的网格来将 aListView和 an 的子项ItemsControl放在行和列中。RowDefinitionsColumnDefinitionsItemsPanel

但是,当我放置时,子控件始终与第 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>
Run Code Online (Sandbox Code Playgroud)

我怎样才能使这项工作?谢谢你。

mm8*_*mm8 6

您应该设置包装根元素的容器的属性Grid.RowGrid.Column附加属性ItemTemplate

<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>
Run Code Online (Sandbox Code Playgroud)

  • 这就是我几周来一直在寻找的东西。多谢! (2认同)