none
ScrollViewer check if visible

    Question

  • Hi, on a ScrollViewer ScrollChanged event I want to check if horizontal or vertical scroll bar was changed.

    In XAML, both Horizontal and Vertical scrollbar visibility is set to Auto. At runtime, how to check if a scrollbar is visible or not?

    <ScrollViewer Name="sv" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" ScrollChanged="sv_ScrollChanged">
        <RichTextBox></RichTextBox>
    </ScrollViewer>

    In code-behind, the HorizontalScrollBarVisibility or  VerticalScrollBarVisibility values return 'Auto' as set in design time, but not how it looks at runtime

    Thanks,

    -srinivas y.


    sri

    Monday, January 13, 2014 1:21 AM

Answers

  • Hi,

    You can use ComputedVerticalScrollBarVisibility and ComputedHorizontalScrollBarVisibility to check it at runtime.

    private void sv_ScrollChanged(object sender, ScrollChangedEventArgs e)
      {
          var scrollViewer = sender as ScrollViewer;
          var horizontalScrollBarVisibility = scrollViewer.ComputedHorizontalScrollBarVisibility;
          var verticalScrollBarVisibility = scrollViewer.ComputedVerticalScrollBarVisibility;
    
          Console.WriteLine(string.Format("Horizontal Visibility: {0}", horizontalScrollBarVisibility));
          Console.WriteLine(string.Format("Vertical Visibility: {0}", verticalScrollBarVisibility));
      }

    XAML

    <ScrollViewer Name="sv" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" ScrollChanged="sv_ScrollChanged">
        <RichTextBox></RichTextBox>
    </ScrollViewer>



    srithar

    • Marked as answer by ysrini Monday, January 13, 2014 5:51 AM
    Monday, January 13, 2014 5:48 AM

All replies

  • Hi ysrini,

    You can check using ComputedHorizontalScrollBarVisibility and ComputedVerticalScrollBarVisibility.

    <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="300" Width="300">
        <ScrollViewer x:Name="sv" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" >
            <Grid Width="200" Height="200" Background="LightBlue" >
                <StackPanel>
                    <TextBlock Text="{Binding Path=ComputedHorizontalScrollBarVisibility,ElementName=sv}" />
                    <TextBlock Text="{Binding Path=ComputedVerticalScrollBarVisibility,ElementName=sv}" />
                </StackPanel>
            </Grid>
        </ScrollViewer>
    </Window>

    個別に明示されていない限りgekkaがフォーラムに投稿したコードにはフォーラム使用条件に基づき「MICROSOFT LIMITED PUBLIC LICENSE」が適用されます。(かなり自由に使ってOK!)

    Monday, January 13, 2014 5:42 AM
  • Hi,

    You can use ComputedVerticalScrollBarVisibility and ComputedHorizontalScrollBarVisibility to check it at runtime.

    private void sv_ScrollChanged(object sender, ScrollChangedEventArgs e)
      {
          var scrollViewer = sender as ScrollViewer;
          var horizontalScrollBarVisibility = scrollViewer.ComputedHorizontalScrollBarVisibility;
          var verticalScrollBarVisibility = scrollViewer.ComputedVerticalScrollBarVisibility;
    
          Console.WriteLine(string.Format("Horizontal Visibility: {0}", horizontalScrollBarVisibility));
          Console.WriteLine(string.Format("Vertical Visibility: {0}", verticalScrollBarVisibility));
      }

    XAML

    <ScrollViewer Name="sv" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" ScrollChanged="sv_ScrollChanged">
        <RichTextBox></RichTextBox>
    </ScrollViewer>



    srithar

    • Marked as answer by ysrini Monday, January 13, 2014 5:51 AM
    Monday, January 13, 2014 5:48 AM