简体   繁体   English

ListView 中的图像展开到 ListView 容器大小保持纵横比

[英]Image in ListView Expand to ListView container size keeping aspect ratio

I am making kind of a picture carousel in my app using a ListView with horizontal scrolling.我正在使用带有水平滚动的ListView在我的应用程序中制作一种图片轮播。

The height of my ListView is binded to the window, so it can change.我的ListView的高度绑定到 window,所以它可以改变。

My images are always way bigger than the height of my view and I can't figure out how to make sure the image dosen't take more space than it has.我的图像总是比视图的高度大得多,我不知道如何确保图像不会占用比它更多的空间。

(height should be = height of the ListView - the height of the Checkbox - height of the ScrollBar ) (高度应该是 = ListView的高度 - Checkbox的高度 - ScrollBar的高度)

<ListView x:Name="listView1" Margin="18,226,15,10" ScrollViewer.VerticalScrollBarVisibility="Disabled" >
   <ListView.ItemTemplate>
      <DataTemplate>
         <StackPanel Orientation="Vertical" CanVerticallyScroll="False" >
            <CheckBox Content="Select" />
            <Image  Source="{Binding Source}" Margin="1" VerticalAlignment="Center" >
               <Image.InputBindings>
                 <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding MouseDoubleClickCommand}" CommandParameter="{Binding Source}"/>
               </Image.InputBindings>
            </Image> 
         </StackPanel>
      </DataTemplate>
   </ListView.ItemTemplate>
   <ListView.ItemsPanel>
      <ItemsPanelTemplate>
         <StackPanel Orientation="Horizontal" CanVerticallyScroll="False"></StackPanel>
      </ItemsPanelTemplate>
   </ListView.ItemsPanel>
</ListView>

The issue is that a StackPanel has no notion of available space.问题是StackPanel没有可用空间的概念。 It just stacks its children and if there is not enough space to accomodate them, they will be cut-off by the containing element.它只是堆叠其子元素,如果没有足够的空间容纳它们,它们将被包含元素切断。

In order to make your images size to fit the available space, you could use a Grid with two rows.为了使您的图像大小适合可用空间,您可以使用两行Grid The first using a Height of Auto to make it take only as much space as the CheckBox needs and the second using the default size of one star to take up the remaining space.第一个使用Auto Height使其仅占用CheckBox所需的空间,第二个使用默认大小的一颗星来占用剩余空间。

Columns and rows that are defined within a Grid can take advantage of Star sizing to distribute remaining space proportionally .Grid中定义的列和行可以利用星型大小来按比例分配剩余空间 When Star is selected as the height or width of a row or column, that column or row receives a weighted proportion of the remaining available space. When Star is selected as the height or width of a row or column, that column or row receives a weighted proportion of the remaining available space.

<DataTemplate>
   <Grid>
      <Grid.RowDefinitions>
         <RowDefinition Height="Auto"/>
         <RowDefinition/>
      </Grid.RowDefinitions>
      <CheckBox Content="Select"/>
      <Image Grid.Row="1" Source="{Binding Source}" Margin="1" VerticalAlignment="Center">
         <Image.InputBindings>
            <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding MouseDoubleClickCommand}" CommandParameter="{Binding Source}"/>
         </Image.InputBindings>
      </Image> 
   </Grid>
</DataTemplate>

Alternatively, you can use a DockPanel with LastChildFill set to true (default).或者,您可以使用将LastChildFill设置为true (默认)的DockPanel

If you set the LastChildFill property to true , which is the default setting, the last child element of a DockPanel always fills the remaining space , regardless of any other dock value that you set on the last child element.如果您将LastChildFill属性设置为true (这是默认设置),DockPanel的最后一个子元素始终填充剩余空间,而不管您在最后一个子元素上设置的任何其他停靠值。

<DataTemplate>
   <DockPanel>
      <CheckBox DockPanel.Dock="Top" Content="Select"/>
      <Image Source="{Binding Source}" Margin="1" VerticalAlignment="Center">
         <Image.InputBindings>
            <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding MouseDoubleClickCommand}" CommandParameter="{Binding Source}"/>
         </Image.InputBindings>
      </Image> 
   </DockPanel>
</DataTemplate>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM