如何让 ListBox 滚动条在 ScrollViewer 中出现?

发布于 2024-12-25 21:32:28 字数 377 浏览 4 评论 0原文

我有一个 ScrollViewer,其中包含一个 DockPanel,其中包含一个 ListBox 作为其填充元素。我面临的问题是,当 ListBox 包含许多项目,并且窗口的高度减小到需要滚动条的程度时,ScrollViewer'滚动条出现,将我的 DockPanel.Dock="Bottom" 控件推离屏幕。 ListBox 的滚动条永远不会出现。相反,当窗口高度减小时,我希望首先出现 ListBox 的滚动条。然后,在 ListBox 缩小到我指定的某个最小高度后,ScrollViewer 的滚动条应该会处理其余的事情。

怎么办?

I have a ScrollViewer, which contains a DockPanel, which contains a ListBox as its filled element. The problem I'm facing is that, when the ListBox contains many items, and the height of the window is reduced to the point where a scrollbar is necessary, the ScrollViewer's scrollbar appears, pushing my controls with DockPanel.Dock="Bottom" off the screen. The ListBox's scrollbar never appears. Instead, when the window height is reduced, I would like the ListBox's scrollbar to appear first. Then, after the ListBox shrinks to some minimum height that I specify, the ScrollViewer's scrollbar should appear to take care of the rest.

How do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

您应该将 ListView 的 MinHeight 属性设置为您希望 ScrollViewer 的 ScrollBar 显示或启用的特定高度。 ListView 的 Height 属性应绑定到 ScrollViewer 的 Height 属性。然后,当窗口的高度减小到隐藏 ListView 中的部分列表的高度时,ListView 的 ScrollBar 就会出现。当 ListView 的高度达到 MinHeight 时,ScrollViewer 的 ScrollBar 就会出现。

这是 Xaml 代码:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ScrollViewer VerticalScrollBarVisibility="Auto">
            <DockPanel>
                <ListView x:Name="listView1" DockPanel.Dock="Bottom" MinHeight="100" Height="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ScrollViewer, AncestorLevel=1}, Path=ActualHeight}"/>
            </DockPanel>
        </ScrollViewer>
    </Grid>
</Window>

You Should set the ListView's MinHeight property to the specific height which you want the ScrollViewer's ScrollBar to apear or being enabled. The ListView's Height Property should be bind to the ScrollViewer's Height Property. Then when the Window's Height is reduced to an Height which is hide some of the list in the ListView, the ListView's ScrollBar apears. when the ListView's Height reaches its MinHeight the ScrollViewer's ScrollBar apears.

This is The Xaml code:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ScrollViewer VerticalScrollBarVisibility="Auto">
            <DockPanel>
                <ListView x:Name="listView1" DockPanel.Dock="Bottom" MinHeight="100" Height="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ScrollViewer, AncestorLevel=1}, Path=ActualHeight}"/>
            </DockPanel>
        </ScrollViewer>
    </Grid>
</Window>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文