Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 959
JDL Avatar asked Jun 11 '13 22:06

JDL


People also ask

How do I add a vertical ScrollBar in WPF?

How to enable scrollbar in a WPF TextBox. The simplest way to add scrolling functionality to a TextBox control is by enabling its horizontal and vertical scrolling. The HorizontalScrollBarVisibility and VerticalScrollBarVisibility properties are used to set horizontal and vertical scroll bars of a TextBox.

How do I set my vertical ScrollBar?

For vertical scrollable bar use the x and y axis. Set the overflow-x:hidden; and overflow-y:auto; that will automatically hide the horizontal scroll bar and present only vertical scrollbar. Here the scroll div will be vertically scrollable.

What is ScrollViewer in WPF?

There are two predefined elements that enable scrolling in WPF applications: ScrollBar and ScrollViewer. The ScrollViewer control encapsulates horizontal and vertical ScrollBar elements and a content container (such as a Panel element) in order to display other visible elements in a scrollable area.


1 Answers

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.

like image 74
Kapitán Mlíko Avatar answered Sep 20 '22 23:09

Kapitán Mlíko