我有一个 ListBox
,其中包含许多 User
项,这些项通过 DataTemplate
d 显示为 UserControl
中的 UserControl
列表框
。每个UserControl
的大小都可以扩展。首先,ListBox
足够大,可以以未展开状态显示它们。我遇到的问题是,当多个 UserControl 一起展开时,它们会超出 ListBox 的可见区域。 ListBox
无法识别这一事实,并且不会出现 ScrollBar
,即使它们设置为 Visible
也是如此。
当用户单击每个动画中的按钮时,我使用 DoubleAnimation 来更改 UserControl 高度。是否有我必须做的事情,或者必须设置 ListBox
上的某些设置才能使其注册代表其项目的 UserControl
的大小更改,并且在需要时显示滚动条?
编辑>>
我已将问题追溯到我在 ListBox.ItemsPanel
中使用的自定义 WrapPanel
。当我删除它或将其替换为标准 WrapPanel
时,ScrollBar
会在需要时出现。我从一篇关于 Panel 的代码rel="nofollow">创建自定义 WPF 面板。任何人都可以看到文章中给出的代码中缺少什么以及为什么它可能会阻止 ScrollBar
的显示?
I have a ListBox
that contains a number of User
items that are DataTemplate
d to appear as UserControl
s in the ListBox
. Each UserControl
can be expanded in size. To start with, the ListBox
is big enough to display them all in their unexpanded state. The problem that I have is that when a number of these UserControl
s are expanded together, they extend out of the ListBox
's visible area. The ListBox
does not recognise this fact and no ScrollBar
s appear, even when they are set to Visible
.
I am using DoubleAnimation
s to alter the UserControl
heights when the user clicks on a button in each one. Is there something that I have to do, or some setting on the ListBox
that must be set to get it to register the size changes of the UserControl
s that represent its items and display ScrollBar
s when needed?
Edit>>>
I have tracked down the problem to a custom WrapPanel
that I am using in the ListBox.ItemsPanel
. When I remove it, or replace it with a standard WrapPanel
, ScrollBar
s appear when required. I got the code for the Panel
from a good article about creating custom WPF panels. Can anyone see what's missing from the code given in the article and why it might stop the ScrollBar
s from displaying?
发布评论
评论(2)
我想知道列表框通常是否符合您的预期?如果您知道项目大小何时发生变化,您可以尝试在 ListBox 上调用 InvalidateMeasure/Layout 来查看?
I wonder whether ListBoxes normally do what you are expecting? You might try calling InvalidateMeasure/Layout on the ListBox if you know when the item sizes change, just to see?
我决定再次完整地编写自定义
WrapPanel
代码,这次它工作正常!将新版本与以前的版本进行比较时,我可以看到测量计算中的 += 中缺少一个 +,因此Panel
认为这些项目比实际小得多。因此没有ScrollBar
。因此,如果您遇到此问题,请仔细检查您的测量代码。
I decided to write the custom
WrapPanel
code again completely and this time it worked correctly! When comparing the new version with the previous version, I could see that a + was missing from a += in a measuring calculation and so thePanel
thought that the items were much smaller than they really were... hence noScrollBar
s.So, if you have this problem, check your measuring code carefully.