ScrollViewer里面的Listbox高度计算 [英] Listbox inside ScrollViewer height calculations

查看:113
本文介绍了ScrollViewer里面的Listbox高度计算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将一个ListBox放置在一个ScrollViewer中时,如果我没有为Lis​​tBox定义一个Height属性,它不会滚动(除了上下跳动),所以屏幕上的元素不可访问。如果我将ListBox上的Height属性设置为它在屏幕上占用的实际高度,它将完美滚动。如果我没有完全正确地确定高度,则它不能正确滚动,例如,



当在ScrollViewer / ListBox上方和下方的单列LayoutRoot Grid中放置其他元素时,我会将其设置为RowDefinition.Height =Auto,除ScrollViewer之外的所有行都获得*。网格似乎相应地分配空间。除了现在我不知道ScrollViewer / ListBox占用了多少空间。

Part A:为什么我必须在ListBox上设置高度,它的(虚拟)高度是否随元素数量而变化?

B部分:看起来我必须手动布置Grid行高,然后在需要调整字体大小或其他样式更改时手动重新执行它们。是这样吗?这种做法似乎是假的。

解决方案

你的问题是由于你在ScrollViewer中有一个ListBox而引起的。不要这样做。



列表框包含一个内部ScrollViewer,并且(通常)会增长到可用空间。



通过在ScrollViewer中基本拥有一个ScrollViewer,它不知道应该增长哪一个以适应可用空间,以及它们应该如何相互滚动。



让我们知道你在做什么。有一个更好的方法来做到这一点。


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.

解决方案

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.

这篇关于ScrollViewer里面的Listbox高度计算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆