Aki24x
Aki24x

Reputation: 1068

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?

Upvotes: 12

Views: 6229

Answers (2)

Semjon Mössinger
Semjon Mössinger

Reputation: 1890

Maybe the default will help you: just don't set Width at all!

At least in my case it was totally sufficient to not set the Width property. While Mårten Wikström's answer is completely correct I found out my actual problem was I had specified (unnecessarily) Width and did this in a wrong way (the same way Aki24x has reported).

This is a simplified case of my example. The solution was to just delete line 4.

1  <ListBox.ItemTemplate>
2      <DataTemplate>
3          <Border
4              Width="..." 
5              >

Upvotes: 0

M&#229;rten Wikstr&#246;m
M&#229;rten Wikstr&#246;m

Reputation: 11344

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

Like this:

Width="{Binding Path=ViewportWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ScrollViewer}}"

Upvotes: 34

Related Questions