ListBox中的WrapPanel行为:vertical用于填充可用空间然后水平包装

时间:2015-08-29 02:02:36

标签: c# .net wpf datatemplate

我在自定义scrollviewer中有一个列表框,它提供了一些独特的视觉样式。列表框ItemsPanel是WrapPanel,所有列表框项目都是100x100。列表框高度是静态的,不会更改。所需的布局是这样的:在包装之前垂直填充可用空间,如下所示:

enter image description here

我使用了WrapPanel的两个Orientation变量,并且无法实现所需的布局。我怀疑这与我在自定义ScrollViewer控件中包装Listbox并且列表框无法正确计算可用空间这一事实有关。由于高度是静态的,我觉得我应该能够做到这一点(它只能水平滚动,而外部自定义滚动查看器应该进行滚动)。以下是当前的设置:

<controls:CustomScrollViewer HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Hidden">
    <controls:CustomScrollViewer.Template> .... </controls:CustomScrollViewer.Template>
    <ListBox Height="...">

        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>

        <ListBox.ItemTemplate>
            <DataTemplate> .... </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.Resources>
            <Style TargetType="{x:Type ListBoxItem}"> .... </Style>
        </ListBox.Resources>
    </ListBox>
</controls:CustomScrollViewer>

有没有办法让ListBox在内部使用自定义scrollviewer,而不是将Listbox包装在scrollviewer中?或者我如何实现所需的布局?

1 个答案:

答案 0 :(得分:1)

通过对WrapPanel的高度进行硬编码,并将方向设置为Vertical,我就能够实现所需的布局。

我仍然会对使用自定义scrollviewer替换列表框内部的scrollviewer的解决方案感到好奇。

相关问题