运行时的 WPF 高度

发布于 2024-11-07 02:19:33 字数 601 浏览 1 评论 0原文

我是 WPF 新手。我试图解决的一个问题是如何在运行时获得正确的高度。

在我的应用程序中,我动态地将用户控件添加到代码后面的堆栈面板中。用户控件包含一些 Texblock。然后,我的 Stackpanel 用作 BookPage 的内容,并将该 BookPage 添加到书籍中(http://wpfbookcontrol.codeplex.com/)。我的 Stackpanel 的高度不应超过某个值。

我已经发现我可以使用 Measure &安排计算ActualSize &用户控件的高度:

itemsa.Measure(new System.Windows.Size(double.PositiveInfinity,     double.PositiveInfinity));
itemsa.Arrange(new Rect(0, 0, 400, itemsa.DesiredSize.Height));

此时,用户控件尚未添加到 Stackpanel 中。 400 是我的用户控件不应超过的宽度,但它确实如此,因为 Textblock 不会创建自动换行符。当我显示这本书时,会创建换行符。

我应该怎么做才能解决这个问题?

提前致谢。

I am new to WPF. A Problem I am trying to solve is how I can get a correct height at runtime.

In my Application I dynamically add Usercontrols to a Stackpanel in the Code behind. The Usercontrol contains a few Texblocks. My Stackpanel is then used as Content for a BookPage and this BookPage is added to a Book(http://wpfbookcontrol.codeplex.com/). The Height of my Stackpanel should not exceed a certain value.

I already figured out that I can use Measure & Arrange to calculate the ActualSize & Height of the Usercontrol:

itemsa.Measure(new System.Windows.Size(double.PositiveInfinity,     double.PositiveInfinity));
itemsa.Arrange(new Rect(0, 0, 400, itemsa.DesiredSize.Height));

At this point the Usercontrol isn't added to the Stackpanel. 400 is the Width my usercontrol shouldn't exceed, but it does, because the Textblock dosn't create automatic linebreaks. When I display the Book the linebreaks are created.

What should I do to solve this Problem?

Thanks in advance.

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

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

发布评论

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

评论(3

云柯 2024-11-14 02:19:33

不确定您是否正在寻找这个,但是使用 MaxHeight 属性您可以限制控件的增长。

一般来说,没有必要覆盖测量和排列,布局系统非常强大,它为您提供了许多容器控件

Not sure if you're looking for this, but with the MaxHeight property you have the possibility to restrict the growing of a control.

In general, it's not necessary to override Measure and Arrange, the Layout-System is very powerfull and it offers you many container-controls that provide specific layout behaviour.

无需解释 2024-11-14 02:19:33

为什么不使用 stackpanel 的 MaxHeight 属性?

ActualWidth 和 ActualHeight 也会告诉您控件的实际大小。

您确定需要所有这些计算吗?我的猜测是,最好使用自动调整 GridRows 大小或类似的东西。

Why not use the property MaxHeight for the stackpanel?

Also ActualWidth and ActualHeight will tell you the actual size of a control.

Are you sure you need all these calculations? My guess is that it would be better to use auto sizing GridRows or something like that.

半葬歌 2024-11-14 02:19:33

在我看来,您想要一个自定义面板以非常具体的方式进行布局。您可以从Panel继承一个新类,然后重写MeasureOverride和ArrangeOverride来确定可以添加什么以及在哪里添加。这也是您用来创建虚拟面板的方法,其中仅创建可见的子面板,并在滚动时将其销毁。

如果您对此感兴趣,请告诉我,我将编辑该帖子并提供一个示例。

It sounds to me like you want a custom panel to do layout in a very specific manner. You can inherit a new class from Panel, and then override MeasureOverride and ArrangeOverride to determine what can be added and where. This is also the method you'd use to do a virtual panel, where only the visible children are created and they are destroyed when scrolled off.

If you are interested in this let me know and I'll edit the post and provide an example.

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