DataTemplate 用于在列中显示其内容的网格的 ItemTemplate

发布于 2024-09-10 05:07:16 字数 2901 浏览 8 评论 0原文

我有 HeaderedItemsControl,其中 ItemsSource 绑定到 ObservableCollection。我想显示它的内容,例如:

*Name*       Müller      Schmid    Weber
*FirstName*  Peter       Hans      Willhelm
*Age*        32          56        78
*Country*    Switzerland Austria   Liechtenstein

到目前为止我的 xaml 代码:

<HeaderedItemsControl ItemsSource="{Binding Path=PersonCollection}">
        <HeaderedItemsControl.Template>
            <ControlTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                     </Grid.RowDefinitions>
                    <TextBlock Grid.Column="0" Grid.Row="0" Text="Name"/>
                    <TextBlock Grid.Column="0" Grid.Row="1" Text="FirstName"/>
                    <TextBlock Grid.Column="0" Grid.Row="2" Text="Age"/>
                    <TextBlock Grid.Column="0" Grid.Row="3" Text="Country"/>
                    <StackPanel Grid.RowSpan="4" Grid.Column="1" Orientation="Horizontal">
                        <ItemsPresenter/>
                    </StackPanel>
                </Grid>
            </ControlTemplate>
        </HeaderedItemsControl.Template>
        <HeaderedItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                    <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition />
                            <RowDefinition />
                            <RowDefinition />
                    </Grid.RowDefinitions>
                        <Label Grid.Row="0" Content="{Binding Path=Name}" />
                        <Label Grid.Row="1" Content="{Binding Path=FirstName}" />
                        <Label Grid.Row="2" Content="{Binding Path=Age}" />
                        <Label Grid.Row="3" Content="{Binding Path=Country}"/>
                </StackPanel>
            </DataTemplate>
        </HeaderedItemsControl.ItemTemplate>
    </HeaderedItemsControl>

这给了我类似的内容:

*Name*       Müller
             Schmid
             Weber
*FirstName*  Peter
             Hans
             Willhelm
*Age*        32
             56 
             78
*Country*    Switzerland
             Austria
             Liechtenstein

有没有办法让项目在列中呈现?

I have HeaderedItemsControl which ItemsSource is bound to a ObservableCollection<Person>. I would like to display it's content like:

*Name*       Müller      Schmid    Weber
*FirstName*  Peter       Hans      Willhelm
*Age*        32          56        78
*Country*    Switzerland Austria   Liechtenstein

My xaml-Code so far:

<HeaderedItemsControl ItemsSource="{Binding Path=PersonCollection}">
        <HeaderedItemsControl.Template>
            <ControlTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                        <RowDefinition />
                     </Grid.RowDefinitions>
                    <TextBlock Grid.Column="0" Grid.Row="0" Text="Name"/>
                    <TextBlock Grid.Column="0" Grid.Row="1" Text="FirstName"/>
                    <TextBlock Grid.Column="0" Grid.Row="2" Text="Age"/>
                    <TextBlock Grid.Column="0" Grid.Row="3" Text="Country"/>
                    <StackPanel Grid.RowSpan="4" Grid.Column="1" Orientation="Horizontal">
                        <ItemsPresenter/>
                    </StackPanel>
                </Grid>
            </ControlTemplate>
        </HeaderedItemsControl.Template>
        <HeaderedItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                    <Grid.RowDefinitions>
                            <RowDefinition />
                            <RowDefinition />
                            <RowDefinition />
                            <RowDefinition />
                    </Grid.RowDefinitions>
                        <Label Grid.Row="0" Content="{Binding Path=Name}" />
                        <Label Grid.Row="1" Content="{Binding Path=FirstName}" />
                        <Label Grid.Row="2" Content="{Binding Path=Age}" />
                        <Label Grid.Row="3" Content="{Binding Path=Country}"/>
                </StackPanel>
            </DataTemplate>
        </HeaderedItemsControl.ItemTemplate>
    </HeaderedItemsControl>

This gives me something like:

*Name*       Müller
             Schmid
             Weber
*FirstName*  Peter
             Hans
             Willhelm
*Age*        32
             56 
             78
*Country*    Switzerland
             Austria
             Liechtenstein

Is there a way to let the items be presented in a column?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心凉 2024-09-17 05:07:16

在 ControlTemplate 中,您已将 ItemsPresenter 包装在 StackPanel 中,在本例中,StackPanel 不执行任何操作,因为它只有一个子项:ItemsPresenter。您真正想要的是 ItemsPresenter 使用 StackPanel 进行其内部布局,您可以使用 ItemsControl.ItemsPanel 来定义用于托管控件生成的项目的面板模板:

<HeaderedItemsControl.ItemsPanel>
  <ItemsPanelTemplate>
    <StackPanel Orientation="Horizontal"/>
  </ItemsPanelTemplate>
</HeaderedItemsControl.ItemsPanel>

您还需要更改 StackPanel 声明您的 ItemTemplate 到 Grid,以便它将每个项目布置到行中。

In your ControlTemplate you've wrapped your ItemsPresenter in a StackPanel, which in this case is doing nothing because it has a single child: the ItemsPresenter. What you actually want is for the ItemsPresenter to use that StackPanel to do its internal layout which you achieve using the ItemsControl.ItemsPanel to define template for a panel that will host the items generated by the control:

<HeaderedItemsControl.ItemsPanel>
  <ItemsPanelTemplate>
    <StackPanel Orientation="Horizontal"/>
  </ItemsPanelTemplate>
</HeaderedItemsControl.ItemsPanel>

You also need to change the StackPanel declaration in your ItemTemplate to Grid so that it will lay out each item into Rows.

往日 2024-09-17 05:07:16

看起来您在 ItemTemplate 中设置了标签的行而不是列,因此您的内容将垂直堆叠。此外,您还可以将标签放置在垂直方向的 StackPanel 中,这将使它们显示在单列中。我猜您正在为 ItemTemplate 寻找这样的布局。

<Grid>
  <Grid.ColumnDefinitions>
    <ColumnDefinition/>
    <ColumnDefinition/>
    <ColumnDefinition/>
    <ColumnDefinition/>
  </Grid.ColumnDefinitions>
  <Label Grid.Column="0" Content="{Binding Path=Name}" />
  <Label Grid.Column="1" Content="{Binding Path=FirstName}" />
  <Label Grid.Column="2" Content="{Binding Path=Age}" />
  <Label Grid.Column="3" Content="{Binding Path=Country}"/>
</Grid>

It looks like within your ItemTemplate you are setting the row of your Labels instead of the columns, so your content would stack vertically. Also, you have the Labels sitting in a StackPanel with vertical orientation, which will make them display in a single column. I'm guessing you are looking for a layout like this for your ItemTemplate.

<Grid>
  <Grid.ColumnDefinitions>
    <ColumnDefinition/>
    <ColumnDefinition/>
    <ColumnDefinition/>
    <ColumnDefinition/>
  </Grid.ColumnDefinitions>
  <Label Grid.Column="0" Content="{Binding Path=Name}" />
  <Label Grid.Column="1" Content="{Binding Path=FirstName}" />
  <Label Grid.Column="2" Content="{Binding Path=Age}" />
  <Label Grid.Column="3" Content="{Binding Path=Country}"/>
</Grid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文