在listview中缩放图像以适应

时间:2016-06-02 12:09:08

标签: wpf listview

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

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

<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>

1 个答案:

答案 0 :(得分:1)

禁用垂直ScrollBar以使ListView垂直缩放其项目:

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

这同样适用于更轻量级ListBox