WPF ListBox 没有意识到它的项目大小已更改

发布于 2024-10-18 01:24:27 字数 953 浏览 14 评论 0 原文

我有一个 ListBox,其中包含许多 User 项,这些项通过 DataTemplated 显示为 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 DataTemplated to appear as UserControls 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 UserControls are expanded together, they extend out of the ListBox's visible area. The ListBox does not recognise this fact and no ScrollBars appear, even when they are set to Visible.

I am using DoubleAnimations 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 UserControls that represent its items and display ScrollBars 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, ScrollBars 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 ScrollBars from displaying?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏の忆 2024-10-25 01:24:27

我想知道列表框通常是否符合您的预期?如果您知道项目大小何时发生变化,您可以尝试在 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?

走走停停 2024-10-25 01:24:27

我决定再次完整地编写自定义 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 the Panel thought that the items were much smaller than they really were... hence no ScrollBars.

So, if you have this problem, check your measuring code carefully.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文