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

时间:2015-10-27 10:41:30

标签: c# wpf

我使用ListBox在我的视图中显示ObservableCollection的内容。在我的简化示例中,我只是为每个项目使用一个图像:

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

HealthCriteriaHealthCriterionViewModel类型的集合。

我得到的是:

enter image description here

我想要的是(拉伸图像以垂直和水平使用所有可用空间):

enter image description here

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

我建议您无法使用ItemsControl实现它。因为内容演示者大小不固定,所以它是可滚动区域。因此应该修复每个元素的大小。它可以直接在ItemsPanel中使用&#34; Weight&#34;或者&#34;身高&#34; (取决于方向)属性或受内容大小限制。

相关问题