WPF 滚动查看器问题

发布于 2024-11-19 23:50:20 字数 2404 浏览 6 评论 0原文

我尝试在程序中使用简单的 ScrollViewer,但遇到问题。

如果我将程序中的所有内容都包含在 ScrollViewer 中,则它可以正常工作:

<Window x:Class="WpfTest.MainWindow"         
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Name="PrimaryWindow">
    <ScrollViewer>
        <StackPanel>
            <Menu Height="21" VerticalAlignment="Top">
                <MenuItem Header="File"/>
                <MenuItem Header="Edit"/>
            </Menu>
            <StackPanel>
                <TextBlock Text="1"/>
                <TextBlock Text="2"/>
                <TextBlock Text="3"/>
                <TextBlock Text="4"/>
                <TextBlock Text="5"/>
                <TextBlock Text="6"/>
                <TextBlock Text="7"/>
                <TextBlock Text="8"/>
                <TextBlock Text="9"/>
                <TextBlock Text="10"/>
            </StackPanel>
        </StackPanel>
    </ScrollViewer>
</Window> 

但是,由于菜单是 ScrollViewer 的一部分,因此当用户向下滚动时,菜单会滚动到屏幕之外。所以我只将ScrollViewer放在菜单下的控件周围:

<Window x:Class="WpfTest.MainWindow"         
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Name="PrimaryWindow">
    <StackPanel>
        <Menu Height="21" VerticalAlignment="Top">
            <MenuItem Header="File"/>
            <MenuItem Header="Edit"/>
        </Menu>
        <ScrollViewer>
            <StackPanel>
                <TextBlock Text="1"/>
                <TextBlock Text="2"/>
                <TextBlock Text="3"/>
                <TextBlock Text="4"/>
                <TextBlock Text="5"/>
                <TextBlock Text="6"/>
                <TextBlock Text="7"/>
                <TextBlock Text="8"/>
                <TextBlock Text="9"/>
                <TextBlock Text="10"/>
            </StackPanel>
        </ScrollViewer>
    </StackPanel>
</Window> 

但是这一次,ScrollViewer不起作用!即,即使我将窗口大小调整为小于标签所需的高度,滚动条也不会被激活。

我做错了什么?

I am trying to use a simple ScrollViewer in my program, but I'm having a problem.

If I enclose everything in my program in a ScrollViewer, it works fine:

<Window x:Class="WpfTest.MainWindow"         
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Name="PrimaryWindow">
    <ScrollViewer>
        <StackPanel>
            <Menu Height="21" VerticalAlignment="Top">
                <MenuItem Header="File"/>
                <MenuItem Header="Edit"/>
            </Menu>
            <StackPanel>
                <TextBlock Text="1"/>
                <TextBlock Text="2"/>
                <TextBlock Text="3"/>
                <TextBlock Text="4"/>
                <TextBlock Text="5"/>
                <TextBlock Text="6"/>
                <TextBlock Text="7"/>
                <TextBlock Text="8"/>
                <TextBlock Text="9"/>
                <TextBlock Text="10"/>
            </StackPanel>
        </StackPanel>
    </ScrollViewer>
</Window> 

However, the since the menu is part of the ScrollViewer, the menu scrolls off the screen when the user scrolls down. So I put the ScrollViewer only around the controls under the menu:

<Window x:Class="WpfTest.MainWindow"         
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Name="PrimaryWindow">
    <StackPanel>
        <Menu Height="21" VerticalAlignment="Top">
            <MenuItem Header="File"/>
            <MenuItem Header="Edit"/>
        </Menu>
        <ScrollViewer>
            <StackPanel>
                <TextBlock Text="1"/>
                <TextBlock Text="2"/>
                <TextBlock Text="3"/>
                <TextBlock Text="4"/>
                <TextBlock Text="5"/>
                <TextBlock Text="6"/>
                <TextBlock Text="7"/>
                <TextBlock Text="8"/>
                <TextBlock Text="9"/>
                <TextBlock Text="10"/>
            </StackPanel>
        </ScrollViewer>
    </StackPanel>
</Window> 

But this time, the ScrollViewer doesn't work! i.e. even if I resize my window to be smaller than the height required by the labels, the scrollbar does not get activated.

What am I doing wrong?

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

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

发布评论

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

评论(3

不知所踪 2024-11-26 23:50:20

问题是由您的根 StackPanel 引起的,StackPanel 不限制 ScrollViewer 的垂直高度。

尝试使用 DockPanel 来定位菜单:

<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<DockPanel>
    <Menu DockPanel.Dock="Top" Height="21" VerticalAlignment="Top">
        <MenuItem Header="File"/>
        <MenuItem Header="Edit"/>
    </Menu>
    <ScrollViewer>
        <StackPanel>
            <TextBlock Text="1"/>
            <TextBlock Text="2"/>
            <TextBlock Text="3"/>
            <TextBlock Text="4"/>
            <TextBlock Text="5"/>
            <TextBlock Text="6"/>
            <TextBlock Text="7"/>
            <TextBlock Text="8"/>
            <TextBlock Text="9"/>
            <TextBlock Text="10"/>
        </StackPanel>
    </ScrollViewer>
</DockPanel>

The problem is caused by your root StackPanel, the StackPanel is not constraining the vertical height of the ScrollViewer.

Try using the DockPanel to position the Menu instead:

<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<DockPanel>
    <Menu DockPanel.Dock="Top" Height="21" VerticalAlignment="Top">
        <MenuItem Header="File"/>
        <MenuItem Header="Edit"/>
    </Menu>
    <ScrollViewer>
        <StackPanel>
            <TextBlock Text="1"/>
            <TextBlock Text="2"/>
            <TextBlock Text="3"/>
            <TextBlock Text="4"/>
            <TextBlock Text="5"/>
            <TextBlock Text="6"/>
            <TextBlock Text="7"/>
            <TextBlock Text="8"/>
            <TextBlock Text="9"/>
            <TextBlock Text="10"/>
        </StackPanel>
    </ScrollViewer>
</DockPanel>

羁〃客ぐ 2024-11-26 23:50:20

仅当祖先元素的高度或宽度更改时,ScrollViewer 才会显示其 Bars。因此,您的祖先是 StackPanel,并且在您调整窗口大小时它不会更改大小。

ScrollViewer will appear its Bars, only when Height or Width of the Ancestor element changed. So, your Ancestor is StackPanel, and it is not changing the size while you resizing the window.

絕版丫頭 2024-11-26 23:50:20

切勿使用内部带有 ScrollViewer 的 StackPanel,因为 StackPanel 的内容想多大就多大!所以ScrollViewer认为它总是有足够的地方!

滚动查看器必须位于所有内容之外

Never use a StackPanel with a ScrollViewer inside it, because the StackPanel is as big as its content wants to be ! So the ScrollViewer thinks it has always enough place !

The scrollViewer must be outside of everything

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文