【问题标题】:WPF ScrollViewer problemWPF ScrollViewer 问题
【发布时间】:2011-07-14 05:45:52
【问题描述】:

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

我做错了什么?

【问题讨论】:

    标签: c# wpf scrollviewer


    【解决方案1】:

    问题是你的根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>
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

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

        scrollViewer 必须在一切之外

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-02-06
          • 1970-01-01
          • 2016-10-03
          • 1970-01-01
          • 1970-01-01
          • 2011-03-24
          相关资源
          最近更新 更多