问题
When I place a ListBox inside a ScrollViewer, if I do not define a Height attribute for the ListBox, it doesn't scroll (aside from bouncing up and down a bit), so elements off the screen are inaccessible. If I set the Height attribute on the ListBox to the actual height it takes up on the screen, it scrolls perfectly. If I don't get the Height exactly right, it doesn't scroll properly, e.g. I might not be able to reach the bottom elements in the list.
When placing other elements in the single column LayoutRoot Grid above and below the ScrollViewer/ListBox, I set the RowDefinition.Height="Auto" on all rows except the ScrollViewer's, which gets "*". The Grid appears to properly allocate space accordingly. Except now I don't know a priori how much space the ScrollViewer/ListBox takes up.
Part A: Why should I have to set the Height on the ListBox, doesn't its (virtual) height vary with the number of elements?
Part B: It appears I have to manually lay out the Grid row heights, then manually re-do them if a fontsize or other style change is called for. Is that the case? That approach seems bogus.
回答1:
Your problems are caused because you've got a ListBox inside a ScrollViewer. Don't do this.
The Listbox contains an internal ScrollViewer and will (normally) grow to the available space.
By essentially having a ScrollViewer inside a ScrollViewer it doesn't know which one should grow to fit available space and how they should scroll relative to one another.
Let us know what you're trying to do. There is a better way to do it.
来源:https://stackoverflow.com/questions/5639098/listbox-inside-scrollviewer-height-calculations