WPF ItemsPanelTemplate
用以定义集合控件的容器外观,如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
学习技术最好的文档就是【官方文档】,没有之一。
还有学习资料【Microsoft Learn】、【CSharp Learn】、【My Note】。
如果,你认为阅读这篇博客让你有些收获,不妨点击一下右下角的【推荐】按钮。
如果,你希望更容易地发现我的新博客,不妨点击一下【关注】。