用以定义集合控件的容器外观,如ListBox,Combox 等等
使用一个自定义的ListBox用以说明,其默认外观是上下排列,这里修改成横向排列
<Window.Resources>
<DataTemplate x:Key="PersonDataTemplate">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Name}"></TextBlock>
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Age}"></TextBlock>
<TextBlock Grid.Row="1" Grid.Column="0" Text="{Binding Hometown}"></TextBlock>
</Grid>
</DataTemplate>
<ItemsPanelTemplate x:Key="PersonItemsPanelTemplate">
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center"></StackPanel>
</ItemsPanelTemplate>
</Window.Resources>
<Grid>
<ListBox ItemsSource="{Binding}" ItemTemplate="{StaticResource PersonDataTemplate}" ItemsPanel="{StaticResource PersonItemsPanelTemplate}"></ListBox>
</Grid>
定义一个ItemsPanelTemplate,设置方向为Horizontal
在ListBox中指定ItemsPanel
效果
常见的条目控件有:ListBox,Menu,StatusBar等(默认纵向排列)
示例代码
https://github.com/zLulus/NotePractice/tree/dev3/WPF/WpfDemo/Template/ItemsPanelTemplate