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

Correct way to set ScrollViewer (for vertical scrolling) on a WPF 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>

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.