Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I bind to a ListBox's viewport width (i.e., width without scrollbar)?

I want to bind to my ListBox's viewport width. That is, the width of the content area, not counting the borders or the scrollbar:

  • When the vertical scroll bar is visible, I want the width of the area between the left border and the scrollbar;
  • when the vertical scroll bar is not visible, I want the width of the area between the left and right borders.

Alternatively, I could make do with with something that tells me whether or not the vertical scrollbar is currently visible.

This is for a control that will be outside the listbox -- I want to position it above the listbox's viewport, and make sure it's always the same width as the viewport. I.e., I want to bind its width to the ListBox's viewport width.

like image 973
Joe White Avatar asked Nov 27 '25 09:11

Joe White


1 Answers

What you want to do is very specific to the internals of the ListBox control template. In the code-behind you can easily find the ScrollViewer and you can then use these useful properties:

  • ScrollViewer.ComputedVerticalScrollBarVisibility
  • ScrollViewer.ViewportWidth

to control the width of your target control. But this tight coupling is characterstic of a composite control and if instead you override the ListBox control template and include your target control at the beginning you will find that you have complete access to the ScrollViewer and its useful properties from XAML and you can associate the widths with a binding without all the problems that trying to maintain independent controls cause.

like image 59
Rick Sladkey Avatar answered Nov 28 '25 22:11

Rick Sladkey