繁体   English   中英

WPF ScrollViewer 问题

[英]WPF ScrollViewer problem

我试图在我的程序中使用一个简单的 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 不起作用。 即,即使我将 window 的大小调整为小于标签所需的高度。 滚动条没有被激活。

我究竟做错了什么?

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

尝试使用 DockPanel 来代替 position 菜单:

<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>

永远不要使用里面有 ScrollViewer 的 StackPanel,因为 StackPanel 的大小和它的内容一样大! 所以 ScrollViewer 认为它总是有足够的地方!

scrollViewer 必须在一切之外

只有当 Ancestor 元素的 Height 或 Width 发生变化时,ScrollViewer 才会出现它的 Bars。 因此,您的祖先是 StackPanel,当您调整 window 的大小时,它不会改变大小。

暂无
暂无

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

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