How to detect ScrollBar is visible on ScrollViewer

Most of time we use <ScrollViewer HorizontalScrollBarVisibility=”Auto”>. Since, it is Auto, we need to tap in some event and do some checking to know scroll bar is appeared.

For this you need to 

<ScrollViewer HorizontalScrollBarVisibility=”Auto” ScrollChanged=”ScrollViewer_ScrollChanged”>
<StackPanel x:Name=”Canvas1″ Width=”800″ Height=”400″>
<Button Click=”ButtonBigger_Click” Width=”100″ HorizontalAlignment=”Left”>Bigger</Button>
<Button Click=”ButtonSmaller_Click” Width=”100″ HorizontalAlignment=”Left”>Smaller</Button>
</StackPanel>
</ScrollViewer>

 

private void ButtonBigger_Click(object sender, RoutedEventArgs e)
{
Canvas1.Width = 888;
Canvas1.Height = 444;
}

private void ButtonSmaller_Click(object sender, RoutedEventArgs e)
{
Canvas1.Width = 333;
Canvas1.Height = 222;
}

private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
bool scrollBarVisible = true;
if (e.ExtentHeight> e.ViewportHeight || e.ExtentWidth>e.ViewportWidth)
{
scrollBarVisible = true;
}
else
{
scrollBarVisible = false;
}
}

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s