在WPF框架上设置ScrollViewer(用于垂直滚动)的正确方法? [英] Correct way to set ScrollViewer (for vertical scrolling) on a WPF Frame?

查看:684
本文介绍了在WPF框架上设置ScrollViewer(用于垂直滚动)的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道这样的框架上定义垂直滚动条的区别吗?

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>

或类似这样:

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

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

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

我将使用第一个选项,因为它可以满足我的需求,但是如果我设置不正确,我不会感到惊讶.

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.

谢谢!

推荐答案

当使用ScrollViewer.VerticalScrollBarVisibilityScrollViewer.HorizontalScrollBarVisibility附加属性时,它对Frame无效.

When you use ScrollViewer.VerticalScrollBarVisibility or ScrollViewer.HorizontalScrollBarVisibility attached property it has no effect with 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>

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

In example above I used both ScrollViewer.VerticalScrollBarVisibility and ScrollViewer.HorizontalScrollBarVisibility attached properties. outcome of that code is the exact opposite of what you would expect. There is no HorizontalScrollBar visible... and you can still see VerticalScrollBar.

这就是为什么这是您应该使用的原因

So that's why this is what you should use

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

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

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

这是以下代码的结果:

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

这是因为这些附加属性现在会影响ListBox中的ScrollViewer,而不像您期望的那样影响父级ScrollViewer.

That's because those attached properties now affect ScrollViewer within ListBox and not parent ScrollViewer as you may expect.

因此从这个小小的实验中,我假定ScrollViewer.VerticalScrollBarVisibility附加属性用于希望能够影响Control模板中存在的ScrollViewer而不是父级ScrollViewer的情况.因此,我认为它不起作用,例如DockPanel.Dock会对父级DockPanel生效.

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

这篇关于在WPF框架上设置ScrollViewer(用于垂直滚动)的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆