DataTemplate 用于在列中显示其内容的网格的 ItemTemplate
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 ControlTemplate 中,您已将 ItemsPresenter 包装在 StackPanel 中,在本例中,StackPanel 不执行任何操作,因为它只有一个子项:ItemsPresenter。您真正想要的是 ItemsPresenter 使用 StackPanel 进行其内部布局,您可以使用 ItemsControl.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:
You also need to change the StackPanel declaration in your ItemTemplate to Grid so that it will lay out each item into Rows.
看起来您在
ItemTemplate
中设置了标签的行而不是列,因此您的内容将垂直堆叠。此外,您还可以将标签放置在垂直方向的 StackPanel 中,这将使它们显示在单列中。我猜您正在为ItemTemplate
寻找这样的布局。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 aStackPanel
with vertical orientation, which will make them display in a single column. I'm guessing you are looking for a layout like this for yourItemTemplate
.