简体   繁体   English

使用 ListBox 和 StackPanel 的所有可用空间作为 ItemsPanelTemplate

[英]Use all available space with ListBox and a StackPanel as ItemsPanelTemplate

I use a ListBox to show the contents of an ObservableCollection in my view.我使用ListBox在我的视图中显示ObservableCollection的内容。 In my simplified example, I just use an image for each item like that:在我的简化示例中,我只为每个项目使用一个图像,如下所示:

<Window>
    <Window.Resources>
        <DataTemplate DataType="{x:Type viewModel:HealthCriterionViewModel}">
            <Image VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Source="Optionsh.png" Stretch="Fill" />
        </DataTemplate>
    </Window.Resources>

    <ListBox ItemsSource="{Binding HealthCriteria}" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Vertical" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    </ListBox>
</Window>

HealthCriteria is a collection of type HealthCriterionViewModel . HealthCriteriaHealthCriterionViewModel类型的HealthCriterionViewModel

What I get is this:我得到的是这样的:

在此处输入图片说明

What I want is this (stretch images to use all available space both vertically and horizontally):我想要的是这个(拉伸图像以在垂直和水平方向使用所有可用空间):

在此处输入图片说明

How can I achieve this?我怎样才能做到这一点?

I suggest you can't achieve it with ItemsControl.我建议你不能用 ItemsControl 实现它。 Because contents presenter size is not fixed, it's scrollable area.由于内容演示者大小不固定,它是可滚动区域。 So size of each element should be fixed.所以每个元素的大小应该是固定的。 It can be limited directly in ItemsPanel with "Weight" or "Height" (depends on orientation) properties or limited by content size.它可以通过“重量”或“高度”(取决于方向)属性直接在 ItemsPanel 中进行限制,或者受内容大小的限制。

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

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