WPF图像仅在一个方向上伸展 [英] WPF Image stretching in only one direction

查看:157
本文介绍了WPF图像仅在一个方向上伸展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个250像素宽的容器,里面有不同尺寸的不同图像。

I have a container at 250px width which houses different images of different sizes.

<Grid width="250">
   <ScrollViewer >
          <StackPanel Orientation="Vertical">
            <ItemsControl ItemsSource="{Binding images}">                                       
              <ItemsControl.Template>
                <DataTemplate>
                  <Image Source="{Binding}" Margin="0,0,5,5" />
                </DataTemplate>
               </ItemsControl.Template>
              </ItemsControl>        
         </StackPanel>
   </ScrollViewer >
</Grid>

有些图像小于250像素,有些图像大于250像素。

Some of the images are less than 250px and some are more than 250px.

我想要发生的是宽度超过250像素的较大图像约束到容器(250像素),同时将图像保持在250px以下的正常宽度。

What I would like to happen is for the larger images over 250px width to constrain to the container (250px) while keeping images under 250px to their normal widths.

如果我这样做会导致250px以下的图像拉伸和填充,但会将较大的图像限制为250px宽度:

If I do this it causes the images under 250px to stretch and fill but constrains the larger images to 250px width:

<Image Source="{Binding}" Margin="0,0,5,5" Stretch="Fill" MaxWidth="250"/>

但如果我这样做会导致250px以下的图像保持正常宽度但图像超过250px t约束并溢出容器外:

But if I do this it causes images under 250px to keep their normal width but images over 250px don't constrain and overflow outside the container:

<Image Source="{Binding}" Margin="0,0,5,5" Stretch="None" MaxWidth="250"/>

有没有解决方法?谢谢。

Is there a way around this? Thanks.

推荐答案

您可以将 StretchDirection 属性设置为 DownOnly

<Image Source="{Binding}" Margin="0,0,5,5" MaxWidth="250"
       Stretch="Uniform" StretchDirection="DownOnly"/>

这篇关于WPF图像仅在一个方向上伸展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆