简体   繁体   English

ListBox with Stackpanel-Items - 如何启用滚动? WPF

[英]ListBox with Stackpanel-Items - How to enable scroll? WPF

I'd like to enable a ScrollView for my ListBox. 我想为我的ListBox启用ScrollView。 My ListBox is showing StackPanels. 我的ListBox显示StackPanels。 Unfortunately I'm not able to set a specific height to my ListBox (this is the only solution I found where I can use my Scroller). 不幸的是我无法为我的ListBox设置一个特定的高度(这是我找到的唯一可以使用我的Scroller的解决方案)。

What do I have to do to get a ScrollBar for my ListBox? 我需要做什么才能为我的ListBox获取ScrollBar? (And even if I minimize/maximize the window it should appear if necessary)... (即使我最小化/最大化窗口,它应该在必要时出现)......

Thanks 谢谢

Well, the ListBox in WPF already contains a scroll, which you can force to be visible like this: 好吧,WPF中的ListBox已经包含一个滚动,您可以强制显示如下:

<ListBox 
    ScrollViewer.VerticalScrollBarVisibility="Visible">
</ListBox>

Most probably your problem comes from the fact, that your listbox resides in a control, which does not delimit it's height, so your listbox has a height that is exactly equal to the needed space (which may be more than your screen estate). 很可能你的问题来自这样一个事实:你的列表框位于一个控件中,它没有界定它的高度,因此你的列表框的高度恰好等于所需的空间(可能超过你的屏幕空间)。 Try to put it inside a Grid for example. 例如,尝试将其放在网格中。

So to illustrate it with an example: 所以用一个例子来说明它:

<StackPanel>
    <ListBox>
        <ListBox.Items>
            <TextBlock>Test</TextBlock>
            ...
        </ListBox.Items>
    </ListBox>
</StackPanel>

will never show a scrollbar, as the stackpanel will have a height that will be always enough for the listbox to show all elements, even though it might not be visible on the screen. 将永远不会显示滚动条,因为堆栈面板的高度始终足以让列表框显示所有元素,即使它可能在屏幕上不可见。 But if you switch the StackPanel to a Grid in the above example you will have scrollbars when they are needed, as the Grid will constrain the height of the child control (the ListBox). 但是如果你在上面的例子中将StackPanel切换到Grid,你需要时会有滚动条,因为Grid会限制子控件(ListBox)的高度。

Hope this helps. 希望这可以帮助。

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

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