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