How to get or compute actual width of ListBox without Vertical Scrollbar if visible
I wonder how to g开发者_JAVA技巧et 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?
Try binding to the ViewportWidth
property of the containing ScrollViewer
instead.
Like this:
Width="{Binding Path=ViewportWidth, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ScrollViewer}}"
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 >
精彩评论