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
-
Hook on to the ListView.LayoutUpdated event
-
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