【发布时间】:2023-04-09 15:28:01
【问题描述】:
我想在 WPF (2) 中创建一个列表视图,它不会在重新调整大小时更改列和行之间的间距,而是自动更改列数以适应。
我有什么:
我想要什么
代码:
<Window x:Class="WPFApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Test1" Height="800" Width="1000" Loaded="Window_Loaded" Closing="Window_Closing">
<Window.Resources>
<DataTemplate x:Key="ItemTemplate">
<WrapPanel>
<Grid>
<Border BorderThickness="3" BorderBrush="#FF000000" VerticalAlignment="Top">
<Image Width="200" Height="300" Stretch="Fill" DataContext="{DynamicResource Image}" Source="{Binding Image}"/>
</Border>
<Image HorizontalAlignment="Right" VerticalAlignment="Top" DataContext="{DynamicResource Data}" Source="{Binding Status}" Height="100"/>
</Grid>
</WrapPanel>
</DataTemplate>
</Window.Resources>
<Grid x:Name="mainGrid" Margin="0,81,0,0">
<ListView HorizontalContentAlignment="Left" VerticalContentAlignment="Top" Name="View" ItemTemplate="{StaticResource ItemTemplate}" ItemsSource="{Binding Path = DataList}">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Margin" Value="5"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Top"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="3"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
</ListView>
</Grid>
【问题讨论】:
-
这看起来像
WrapPanel的效果。您是否尝试将其分配为ItemsPanelTemplate? -
如果我将
WarpPanel分配给ItemsPanelTemplate它可以工作,但它只显示一行或一列,而不是两者
标签: c# wpf listview resize alignment