Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get or compute actual width of ListBox without Vertical Scrollbar if visible

I wonder how to get or compute actual width of ListBox without Vertical Scrollbar if visible.

What I want to do is changing width of each items inside ListBox without being covered by Vertical Scrollbar.

Width="{Binding ActualWidth, 
    RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}

At least, above binding tell me the actual width of ListBox, but this one does not handle width of vertical scrollbar.

Is there any good way to solve this?

like image 350
Aki24x Avatar asked Apr 02 '11 22:04

Aki24x


People also ask

Does view width include scrollbar?

Many assume that width: 100vw is the same as width: 100% . This is true on a page that doesn't scroll vertically, but what you might not realize is that the viewport actually includes the scrollbars, too.

How is scrollbar width calculated?

ClientWidth defines the width of an element. So scrollbar can be defined as width = offsetWidth – clientWidth.

How can ListBox be made to scroll smoothly?

Scrolling behavior in a ListBox is provided by a ScrollViewer. By default, the CanContentScroll property of the containing ScrollViewer is set to true, indicating that the items panel (e.g. a StackPanel) is responsible for the scrolling. The StackPanel scrolls one item at a time as you drag the scrollbar thumb.

Does VW include scrollbar?

No, there's no way to calculate the vw without the scrollbars in CSS. As you can see in the example above, the row--full-width element bleeds out of the container, and it aligns with the header even when there's a scrollbar.


1 Answers

Try binding to the ViewportWidth property of the containing ScrollViewer instead.

Like this:

Width="{Binding Path=ViewportWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ScrollViewer}}"
like image 52
Mårten Wikström Avatar answered Oct 11 '22 13:10

Mårten Wikström