Welcome to WindowsClient.net | Sign in | Join
in

WindowsClient.net

Learnings..

(WPF)Figure out dynamically if the ListView ScrollViewer is showing!!

I recently faced a situation where i had a databound ListView with a ItemTemplate consisting of a text block.The ListView is of a predefined height and width and the TextBlock is textwrapped and set to the same width as the ListView..

Everything worked fine till the number of items in the ListView increased resulting in the Default ScrollViewer of the ListView showing up showing Vertical as well as horizontal scroll bars.The annoying aspect is that the scrollviewer uses the real estate of the ListView...

So now the TextBlock can no longer be seen completely as it is hidden behind the vertical scrollbar and i need to use the horizontal scroll bar to view the complete text...I am sure many have faced this particular issue...

So one way around is to check if the ScrollBar can be seen and if so then adjust the width of the text block.This is all fine except that there is no direct way to extract the scrollviewer of the ListView....

The following steps can help you

  1. Hook on to the ListView.LayoutUpdated event
  2. The following lines of code in the LayoutUpdated event handler help extract the ScrollViewer(consider the Listview is called listView)
Decorator border = VisualTreeHelper.GetChild(listView, 0) as Decorator;//Border is the first child of a Listview

ScrollViewer scroll = border.Child as ScrollViewer;

     3.  Now we need to figure out of the ScrollBar is visible or not. ScrollViewer.VerticalScrollBarVisibility or ScrollViewer.IsVisible etc all proved useless in this case.

ScrollViewer.VerticalScrollBarVisibility is Auto in this case and will show the value of Auto whether its seen or not. ScrollViewer.IsVisible will return true as we have not set it to false coz we want it to show up as and when required.

The only property i could make use of here to figure out if the scrollViewer is showing a vertical scrollbar is

ScrollViewer.ScrollableHeight. If the value is set to 0 then the scrollbar is not visible and if it is greater than 0 then it is visible.

So using these steps i could figure out when i need to resize my textblock to prevent it from hiding behind the vertical scroll bar.

Somehow i am not  very happy  about the use of ScrollViewer.ScrollableHeight but thats all i could find to help me...if you do figure out a better way...do drop me a line:-)

 Till then..

Anshulee

 

Comments

 

Praveen Miranda said:

Excellent just what I was looking for :)

April 16, 2008 11:49 PM
 

Oliver said:

Thanks this is very useful for my project which has the same requirement.

August 27, 2008 7:50 AM
 

Mark said:

Hi.

Your example is great, but ListView.LayoutUpdated event fires very often. I have reach to ScrollBar in ListView and used IsVisibleChanged event and I think this is better solution. What do you think?

September 24, 2008 3:25 AM
 

Rory said:

Take a look at ScrollViewer.ComputedVerticalScrollBarVisiblity, it might be the property you're looking for.

October 20, 2008 5:09 AM

Leave a Comment

(required)  
(optional)
(required)  
Add
Page view counter