【问题标题】:WPF - Resizing Column/Row aligmentWPF - 调整列/行对齐
【发布时间】: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


【解决方案1】:

我找到了解决方案,我改变了这个:

 <ItemsPanelTemplate>
            <UniformGrid Columns="3"/>
 </ItemsPanelTemplate>

到这里:

  <ItemsPanelTemplate>                 
                <UniformGrid  VerticalAlignment="Top" HorizontalAlignment="Left" Columns="3"/>
  </ItemsPanelTemplate>

【讨论】:

  • 调整大小时是否显示第 4 列?我很好奇,因为您明确指定了Columns=3
猜你喜欢
  • 1970-01-01
  • 2017-01-29
  • 1970-01-01
  • 1970-01-01
  • 2016-03-15
  • 2012-01-28
  • 1970-01-01
  • 2011-01-28
  • 1970-01-01
相关资源
最近更新 更多