locked
Scrollviewer scrollbar visible event? RRS feed

  • Question

  • I bind my data collection to ItemsControl. I have wrapped this ItemsControl in a ScrollViewer. Depending on the number of items, the ScrollViewer will show or hide the scrollbar.This ItemsControl is part of a bigger window. When the items exceed the minimum height, then the scrollbar will be visible for the ItemsControl. Since this is a small portion of the window, it is very easy to miss the scrollbar and ignore the rest of the items which are visible only on scrolling.

    I want to add a border of thickness 1 to this ScrollViewer when the scrollbar is visible (drawing the user's attention that there are more items). If the scrollbar is not visible, then the border thickness should be 0. I can't find any event which can tell me whether scrollbar is visible. Is there a workaround for this?

    Friday, February 5, 2010 1:28 AM

Answers

  • Hello anaamica,

    I'm afraid there's no event that triggers itself when the verticalScrollBar appears, but you can solve it by a simple comparation:

    - Get the scrollviewer.SizeChanged event

    scrviewer.SizeChanged += new SizeChangedEventHandler(scrviewer_SizeChanged);

    - On the event handling function, compare if the scrollviewer.actualheight (which will return the height the scrollviewer has on display) is lower than the scrollviewer.scrollableheight (which will return the scrollviewer total content height). If the scrollableheight is larger than its actualheight, then just set borderthicness="1". Otherwise, set it to "0".

    void scrviewer_SizeChanged(object sender, SizeChangedEventArgs e)

    {

    this.LayoutRoot.UpdateLayout(); //just to check that the scrollviewer has been rendered and the app know what size is it.

    if(this.scrViewer.Actualheight < this.scrViewer.ScrollableHeight)

    {

    this.scrViewer.BorderThickness = new Thickness(1,1,1,1);

    }

    else

    {

    this.scrViewer.BorderThickness = new Thickness(0,0,0,0);

    }

    }

    Hope it helps!

    Friday, February 5, 2010 1:10 PM

All replies

  • Hello anaamica,

    I'm afraid there's no event that triggers itself when the verticalScrollBar appears, but you can solve it by a simple comparation:

    - Get the scrollviewer.SizeChanged event

    scrviewer.SizeChanged += new SizeChangedEventHandler(scrviewer_SizeChanged);

    - On the event handling function, compare if the scrollviewer.actualheight (which will return the height the scrollviewer has on display) is lower than the scrollviewer.scrollableheight (which will return the scrollviewer total content height). If the scrollableheight is larger than its actualheight, then just set borderthicness="1". Otherwise, set it to "0".

    void scrviewer_SizeChanged(object sender, SizeChangedEventArgs e)

    {

    this.LayoutRoot.UpdateLayout(); //just to check that the scrollviewer has been rendered and the app know what size is it.

    if(this.scrViewer.Actualheight < this.scrViewer.ScrollableHeight)

    {

    this.scrViewer.BorderThickness = new Thickness(1,1,1,1);

    }

    else

    {

    this.scrViewer.BorderThickness = new Thickness(0,0,0,0);

    }

    }

    Hope it helps!

    Friday, February 5, 2010 1:10 PM
  • You can use the ComputedVerticalScrollBarVisibility property of ScrollViewer (inside a DataTrigger in XAML), which will be System.Windows.Visibility.Visible if the vertical scroll is visible.
    • Edited by Aviran Katz Sunday, February 19, 2017 2:02 PM Added information
    Sunday, February 19, 2017 2:00 PM