WPF图像仅向一个方向拉伸

WPF图像仅向一个方向拉伸,wpf,image,xaml,Wpf,Image,Xaml,我有一个250像素宽的容器,里面有不同大小的图像 <Grid width="250"> <ScrollViewer > <StackPanel Orientation="Vertical"> <ItemsControl ItemsSource="{Binding images}"> <It

我有一个250像素宽的容器,里面有不同大小的图像

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

我希望发生的是,对于大于250px宽度的较大图像,将其约束到容器(250px),同时将小于250px的图像保持在其正常宽度

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

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

但如果我这样做,则会导致低于250px的图像保持其正常宽度,但高于250px的图像不会约束并溢出到容器外:

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


有办法解决这个问题吗?谢谢。

您可以将
StretchDirection
属性设置为
DownOnly

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