【问题标题】:Scale images in listview to fit缩放列表视图中的图像以适合
【发布时间】:2016-10-02 04:02:26
【问题描述】:

我正在尝试制作一个列表视图,其中每个项目都是一个图像。我希望列表视图水平显示项目。如果视图框项目不能水平放置在窗口中,我想要一个水平滚动条。如果列表项不能垂直适合窗口,我希望图像按比例缩小以使其适合。我似乎在列表视图上获得了一个垂直滚动条,而不是缩放图像以适应。

当窗口垂直调整大小时,它会导致垂直滚动条出现在列表视图上。我尝试了各种将图像高度设置为祖先列表视图高度的选项,但我无法使其正常工作。如何实现我想要的行为?

<Window x:Class="ViewBoxExample.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ViewBoxExample"
        mc:Ignorable="d"
        d:DataContext="{d:DesignInstance Type=local:MainWindow}"
        Title="Viewbox Test"
        Height="400" Width="600">
    <Window.Resources>
        <DataTemplate x:Key="ItemTemplate" >
                <Border BorderBrush="Black" BorderThickness="2" >
                    <Image Margin="2" Source="image.png" />
                </Border>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
       </Grid.RowDefinitions>
        <ListView VerticalAlignment="Stretch"
                  ItemTemplate="{StaticResource ItemTemplate}"
                  ItemsSource="{Binding Items}">
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal">
                    </StackPanel>
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>
        </ListView>
        <Grid Grid.Row="1" Height="100">
            <!--placeholder for more content-->
        </Grid>
    </Grid>
</Window>

【问题讨论】:

    标签: wpf listview


    【解决方案1】:

    禁用垂直滚动条以使 ListView 垂直缩放其项目:

    <ListView ItemsSource="{Binding Items}"
              ItemTemplate="{StaticResource ItemTemplate}"
              ScrollViewer.VerticalScrollBarVisibility="Disabled">
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
    </ListView>
    

    这也适用于更轻量级的ListBox

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-31
      • 1970-01-01
      • 1970-01-01
      • 2012-04-17
      • 2012-07-26
      • 2019-04-08
      相关资源
      最近更新 更多