我想创建一个包含3列的ListView,我在ListView标签中创建一个GridView,但这并没有为我提供我想要的相同结果。
附上我想要的图片。
这是我正在使用的一段代码。
[![<ListView Name="List1" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,70,0,0" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Hidden" Background="Transparent" BorderThickness="0" RenderTransformOrigin="-1.33,-0.562" SelectionChanged="ListView_SelectionChanged">
<ListView.View>
<GridView ColumnHeaderContainerStyle="{StaticResource myHeaderStyle}">
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<DockPanel HorizontalAlignment="Right" Height="200" Width="200" LastChildFill="False" VerticalAlignment="Top">
<StackPanel Orientation="Vertical">
<Grid>
<Label Content="{Binding username}" Width="100" Height="200" VerticalAlignment="Bottom" HorizontalAlignment="Center"/>
<Image Source="{Binding imageSource}" Width="100" Height="100"/>
</Grid>
</StackPanel>
</DockPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<DockPanel HorizontalAlignment="Right" Height="200" Width="200" LastChildFill="False" VerticalAlignment="Top">
<StackPanel Orientation="Vertical">
<Grid>
<Label Content="{Binding username}" Width="100" Height="200" VerticalAlignment="Bottom" HorizontalAlignment="Center"/>
<Image Source="{Binding imageSource}" Width="100" Height="100"/>
</Grid>
</StackPanel>
</DockPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<DockPanel HorizontalAlignment="Right" Height="200" Width="200" LastChildFill="False" VerticalAlignment="Top">
<StackPanel Orientation="Vertical">
<Grid>
<Label Content="{Binding username}" Width="100" Height="200" VerticalAlignment="Bottom" HorizontalAlignment="Center"/>
<Image Source="{Binding imageSource}" Width="100" Height="100"/>
</Grid>
</StackPanel>
</DockPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>][1]][1]
答案 0 :(得分:3)
更好的方法是使用UniformGrid
作为ItemsPanel
的{{1}}。
ListView
以下是使用<ListView>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="3" />
</ItemsPanelTemplate>
</ListView.ItemsPanel>
...
</ListView>
代替ListBox
执行此操作的示例,如评论中所述。这是我推荐的方式,除非你需要ListView
的一些特殊功能,因为它会更高效。
这使用基础ListView
类的AgentOctal.WpfLib NuGet包(我是此包的作者)来提供属性更改通知和命令支持。您应该能够替换正确实现ViewModel
的任何工作方法。
XAML:
INotifyPropertyChanged
查看模型:
<Window
x:Class="ColumnsTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ListViewColumns"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="800"
Height="450"
mc:Ignorable="d">
<Window.DataContext>
<local:MainWindowVm />
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ListBox
Grid.Row="0"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch"
ItemsSource="{Binding Path=People}">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="3" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Border
Height="{Binding RelativeSource={RelativeSource Self}, Path=ActualWidth}"
Margin="4"
BorderBrush="Black"
BorderThickness="1">
<TextBlock Text="{Binding Path=Name}" />
</Border>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button
Grid.Row="1"
Command="{Binding Path=AddPersonCommand}"
Content="Add Person" />
</Grid>
</Window>