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

JDL picture JDL · Jun 12, 2013 · Viewed 49.8k times · Source

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

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!

Answer

Kapit&#225;n Ml&#237;ko picture Kapitán Mlíko · Jun 12, 2013

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>

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>

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

This is the result of following code:

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 ListBox and not parent ScrollViewer as you may expect.

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.