在 WPF 框架上设置 ScrollViewer(用于垂直滚动)的正确方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17054824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:50:20  来源:igfitidea点击:

Correct way to set ScrollViewer (for vertical scrolling) on a WPF Frame?

wpfscrollbarwebbrowser-controlframescrollviewer

提问by JDL

does anyone know the difference between defining a vertical scrollbar on a frame like this:

有谁知道在这样的框架上定义垂直滚动条的区别:

        <ScrollViewer Grid.Row="2" VerticalScrollBarVisibility="Auto">
            <Frame Name="Frame1"
                   ScrollViewer.CanContentScroll="True" />
        </ScrollViewer>

or like this:

或者像这样:

        <ScrollViewer Grid.Row="2">
            <Frame Name="Frame1"
                   ScrollViewer.VerticalScrollBarVisibility="Auto"
                   ScrollViewer.CanContentScroll="True" />
        </ScrollViewer>

This frame is nested in a WebBrowser control and setting it the first way correctly displays the vertical scrollbar and is only visible when it needs to scroll (auto). When I set it the second way the vertical scrollbar works but is always visible even when it does not need to scroll (visible).

此框架嵌套在 WebBrowser 控件中,并将其设置为第一种方式正确显示垂直滚动条,并且仅在需要滚动(自动)时可见。当我以第二种方式设置它时,垂直滚动条工作但即使不需要滚动(可见)也始终可见。

I am going to use the 1st option because it meets my needs, but I don't want to be surprised down the road if I am setting it incorrectly.

我将使用第一个选项,因为它满足我的需求,但如果我的设置不正确,我不想在路上感到惊讶。

Thanks!

谢谢!

回答by Kapitán Mlíko

When you use ScrollViewer.VerticalScrollBarVisibilityor ScrollViewer.HorizontalScrollBarVisibilityattached property it has no effect with Frame.

当您使用ScrollViewer.VerticalScrollBarVisibilityScrollViewer.HorizontalScrollBarVisibility附加属性时,它对 Frame 没有影响。

<ScrollViewer Margin="225.667,-4,0,296.939" HorizontalAlignment="Left" Width="221.667">
        <Frame Content="Frame" ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Hidden" Source="UserControl2.xaml" Background="#FFDE5454"/>
</ScrollViewer>

In example above I used both ScrollViewer.VerticalScrollBarVisibilityand ScrollViewer.HorizontalScrollBarVisibilityattached properties. outcome of that code is the exact opposite of what you would expect. There is no HorizontalScrollBarvisible... and you can still see VerticalScrollBar.

在上面的示例中,我同时使用了ScrollViewer.VerticalScrollBarVisibilityScrollViewer.HorizontalScrollBarVisibility附加属性。该代码的结果与您期望的完全相反。没有HorizontalScrollBar可见的……你仍然可以看到VerticalScrollBar

So that's why this is what you should use

所以这就是为什么这是你应该使用的

<ScrollViewer Grid.Row="2" VerticalScrollBarVisibility="Auto">
        <Frame Name="Frame1" />
</ScrollViewer>

When you try this for example with ListBox then result will be different.

例如,当您使用 ListBox 尝试此操作时,结果会有所不同。

This is the result of following code:

这是以下代码的结果:

enter image description here

enter image description here

<ScrollViewer Margin="225.667,0,0,12.761" Height="280.178" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="221.667">
        <ListBox ScrollViewer.CanContentScroll="True" ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Visible" Background="Orange" ItemsSource="{Binding Collection}" DisplayMemberPath="Property1" />
</ScrollViewer>

That's because those attached properties now affect ScrollViewer within ListBoxand not parent ScrollVieweras you may expect.

那是因为这些附加属性现在影响 ScrollViewer 内部ListBox而不是ScrollViewer您可能期望的父级。

So from this little experiment I assume that ScrollViewer.VerticalScrollBarVisibilityattached property is meant for cases where you want to be able to affect ScrollViewerwhich exists within Control's template and not parent ScrollViewer. So I think it does not work for example as DockPanel.Dockwhich takes effect on parent DockPanel.

因此,从这个小实验中,我假设ScrollViewer.VerticalScrollBarVisibility附加属性适用于您希望能够影响ScrollViewerControl 模板中存在的内容而不是 parent 的情况ScrollViewer。所以我认为它不起作用,例如DockPanel.Dock对 parent 生效DockPanel