C# 如何将图像大小限制为其父容器

C# 如何将图像大小限制为其父容器,c#,wpf,image,C#,Wpf,Image,我正在用MVVM模型做我的第一个WPF项目。我有两种观点,我想这样看: 但遗憾的是,图像没有缩小到父级大小,而是保持原始大小: 风景画家 <UserControl x:Class="BIF.Views.PictureList" xmlns:view="clr-namespace:BIF.Views" d:DesignHeight="120" d:DesignWidth="300"> <ListBox ItemsSource="{

我正在用MVVM模型做我的第一个WPF项目。我有两种观点,我想这样看:

但遗憾的是,图像没有缩小到父级大小,而是保持原始大小:

风景画家

<UserControl x:Class="BIF.Views.PictureList"
         xmlns:view="clr-namespace:BIF.Views"
         d:DesignHeight="120" d:DesignWidth="300">

    <ListBox ItemsSource="{Binding List}" <!-- List<PictureViewModel> -->
        ScrollViewer.VerticalScrollBarVisibility="Hidden" 
        SelectedItem="{Binding CurrentPicture, Mode=TwoWay}" SelectionMode="Single">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <ListBoxItem>
                    <view:Picture DataContext="{Binding}" />
                </ListBoxItem>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</UserControl>
但这不是100%正确,我也不想设置固定高度,因为我希望PictureList可以调整大小


欢迎您提出任何建议。

您必须将ScrollViewer.VerticalScrollBarVisibility设置为“Disabled”,因为“Hidden”值在垂直方向为内容提供无限空间,仅此而已。

StackPanel不限制容器的大小,因此所有空间都可供其子元素使用

您应该为堆栈面板设置限制,或者在这种情况下使用更好的控件,可能是网格


如果您在这方面需要帮助,请告诉我。

两个小小的变化,现在一切都很好

  • ScrollViewer.VerticalScrollBarVisibility=“已禁用”
  • DataTemplate中没有ListItem

    <ListBox ItemsSource="{Binding List}" <!-- List<PictureViewModel> -->
        ScrollViewer.VerticalScrollBarVisibility="Disabled" 
        SelectedItem="{Binding CurrentPicture, Mode=TwoWay}" SelectionMode="Single">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <!-- No ListItem here -->
                <view:Picture DataContext="{Binding}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    
    ScrollViewer.VerticalScrollBarVisibility=“已禁用”
    SelectedItem=“{Binding CurrentPicture,Mode=TwoWay}”SelectionMode=“Single”
    


  • 谢谢你们

    在XAML中,没有用于绑定
    实际高度的
    StackPanel
    。请注意,在DataTemplate(ItemTemplate属性)中不应该有
    ListBoxItem
    。您可以使用
    Picture
    控件作为DataTemplate的直接子级。无需在DataTemplate中包含ListBoxItem。模板的内容插入到ListBoxItem中,因此您将以LBI中的LBI结束。在运行时使用Snoop查看此内容(获取Snoop!您会感谢我的)。此外,虽然您的要求阻止了这项工作的正常进行(您的高度是统一的,但宽度不是统一的),但您可以使用UniformGrid,并且已经完成了:/
    Height="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type StackPanel}}, Path=ActualHeight}"
    
    <ListBox ItemsSource="{Binding List}" <!-- List<PictureViewModel> -->
        ScrollViewer.VerticalScrollBarVisibility="Disabled" 
        SelectedItem="{Binding CurrentPicture, Mode=TwoWay}" SelectionMode="Single">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Horizontal" />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <!-- No ListItem here -->
                <view:Picture DataContext="{Binding}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>