WPF:ItemsPanelTemplate中的WrapPanel扩展列表宽度

WPF:ItemsPanelTemplate中的WrapPanel扩展列表宽度,wpf,size,parent,wrappanel,Wpf,Size,Parent,Wrappanel,我有一个列表框,定义如下: <ListBox.ItemTemplate> <DataTemplate> <ItemsControl> <!-- Contents here --> </ItemsControl> </DataTemplate> </ListBox.ItemTemplate>

我有一个列表框,定义如下:

    <ListBox.ItemTemplate>
        <DataTemplate>
            <ItemsControl>
                <!-- Contents here -->
            </ItemsControl>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

    <ListBox.ItemTemplate>
        <DataTemplate>
            <ItemsControl>
                <!-- Contents here -->
            </ItemsControl>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

我的问题是:这个列表包含在一个网格控件中,应该使用它所包含的单元格的所有可用空间,但它不应该强制父级分配更多空间。但发生的情况是,一旦“包裹”面板填满,它不会实际将项目包裹到下一行(应该是这样),而是会扩展列表框的宽度,并在此过程中强制父网格调整大小

    <ListBox.ItemTemplate>
        <DataTemplate>
            <ItemsControl>
                <!-- Contents here -->
            </ItemsControl>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
如何使包裹面板尊重其父面板的大小,而不强制其扩展其大小

    <ListBox.ItemTemplate>
        <DataTemplate>
            <ItemsControl>
                <!-- Contents here -->
            </ItemsControl>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
提前谢谢

    <ListBox.ItemTemplate>
        <DataTemplate>
            <ItemsControl>
                <!-- Contents here -->
            </ItemsControl>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

编辑:还有一件事。我可以显式设置wrappanel的宽度以使其包裹,但我希望wrappanel的大小与列表框的大小相同。

set
ScrollViewer.HorizontalScrollBarVisibility=“Disabled”
wrappanel
将包裹

    <ListBox.ItemTemplate>
        <DataTemplate>
            <ItemsControl>
                <!-- Contents here -->
            </ItemsControl>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
<Grid>
    <ListBox
        ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
    </ListBox>
</Grid>

我已经解决了这个问题。。问题是(可能是)包含我的listbox的一些容器在为我的listbox分配空间方面有点过于慷慨。 在任何情况下,只要我删除了其余容器(包含我的列表)的滚动条,它就会按预期工作。:)

    <ListBox.ItemTemplate>
        <DataTemplate>
            <ItemsControl>
                <!-- Contents here -->
            </ItemsControl>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

感谢您的回复,这对解决此问题非常有帮助。

我在设计模式(Blend和VS 2013)中处理此问题,直到我在页面中添加了一些设计时大小调整,它才结束:

    <ListBox.ItemTemplate>
        <DataTemplate>
            <ItemsControl>
                <!-- Contents here -->
            </ItemsControl>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
d:DesignHeight=“300”
d:DesignWidth=“600”

通常,在将水平滚动条设置为禁用之前,它不会像您预期的那样自动换行。现在您有了一个实际包装的包装袋:)

    <ListBox.ItemTemplate>
        <DataTemplate>
            <ItemsControl>
                <!-- Contents here -->
            </ItemsControl>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

不幸的是,这不起作用。。。我已尝试将其设置为隐藏和禁用,但它仍会扩展其使用的空间:(请注意,问题从来不是列表框变得比它所在的网格大(因此会显示滚动条),问题是它所在的网格单元被放大了,列表框也被放大了。哦,还有一件事。网格包含在一个uniformgrid中,这可能是问题吗?也许uniformgrid不包含网格,因此会扩展。在您的问题中,您提到了wrappanel,而不是uniformgrid。我的代码修复了您在中解释的问题你关于wrappanel的问题。如果你在列表框中的uniformgrid作为itemspanel也有问题,你应该写另一个问题。我想你误解了我…问题是关于wrappanel没有包装。结构如下:uniformgrid->Grid->ListBox with wrappanel