如何在渲染之前测量 WPF 中 TextBlock 的大小?

发布于 2024-09-04 11:40:01 字数 203 浏览 2 评论 0原文

我有一个 WPF DataTemplate,其中有两个 TextBlock 控件(堆叠),下面还有一些其他元素。由于一些复杂的布局代码,我需要知道两个 TextBlock 元素的高度,以便我可以绘制一些奇特的连接线,并排列其他控件等。

如果我知道要进入 TextBlock 的文本,我就知道字体等,是否有某种方法可以计算或测量这些 TextBlock 的高度而不实际渲染它们?

I have a WPF DataTemplate with two TextBlock controls (stacked) and then some other elements underneath. Due to some complicated layout code, I need to know the height of the two TextBlock elements so that I can draw some fancy connector lines, and line up other controls, etc.

If I know the text that's going into the TextBlocks, and I know the font, etc., is there some way I can compute or measure the height of these TextBlocks without actually rendering them?

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

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

发布评论

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

评论(5

想你只要分分秒秒 2024-09-11 11:40:01

我认为调用 UIElement 就足够了.Measure(Size) 方法,然后检查 UIElement.DesiredSize 属性。有关详细信息,请查看提供的 MSDN 链接。

I think it should be sufficient to call the UIElement.Measure(Size) method and subsequently check the UIElement.DesiredSize property. For more information, check the provided MSDN links.

摇划花蜜的午后 2024-09-11 11:40:01

UIElement.Measure(Size) 的调用将 Size 作为参数。第二次调用 UIElement.DesiredSize 返回您传递到 Measure 方法中的任何 Size

我认为是这种情况,因为 UIElement (在本例中为 TextBlock)还不是任何控件的子级,因此 DesiredSize 没有有理由变得不同。

The call to UIElement.Measure(Size), takes as a parameter Size. The second call UIElement.DesiredSize returns whatever Size you passed into the Measure method.

I think this is the case because UIElement (TextBlock in this case) is NOT a child of any control (yet) and therefore DesiredSize has no reason to be anything different.

亢潮 2024-09-11 11:40:01

我知道这是一个相当老的问题,但我发现使用以下代码

        TextBlock textBlock = new TextBlock();
        textBlock.Text = "NR valve";
        Size msrSize = new Size(100, 200);
        textBlock.Measure(msrSize);
        Size dsrdSize = textBlock.DesiredSize;

dsrdSize 返回为 {47.05,15.96}。
诀窍似乎是使 msrSize 大于预期的实际大小。 msrSize 似乎充当 DesiredSize() 结果的限制。
例如,使用 msrSize = new Size(10, 10) 会导致 dsrdSize 为 {10,10}。
希望这对某人有帮助。

I appreciate that this is a rather old question, but I have found that using the following code

        TextBlock textBlock = new TextBlock();
        textBlock.Text = "NR valve";
        Size msrSize = new Size(100, 200);
        textBlock.Measure(msrSize);
        Size dsrdSize = textBlock.DesiredSize;

dsrdSize is returned as {47.05,15.96}.
The trick seems to be making the msrSize larger than the expected actual size. msrSize seems to act as a limit for the DesiredSize() result.
For example, using msrSize = new Size(10, 10), results in a dsrdSize of {10,10} here.
Hope this helps someone.

猥︴琐丶欲为 2024-09-11 11:40:01
public static Size ShapeMeasure(TextBlock tb) {
    // Measured Size is bounded to be less than maxSize
    Size maxSize = new Size(
         double.PositiveInfinity, 
         double.PositiveInfinity);
    tb.Measure(maxSize);
    return tb.DesiredSize;
}

public static Testit() 
{
    TextBlock textBlock = new TextBlock();
    textBlock.Text = "NR valve";

    Size text size = ShapeMeasure(textBlock);
}
public static Size ShapeMeasure(TextBlock tb) {
    // Measured Size is bounded to be less than maxSize
    Size maxSize = new Size(
         double.PositiveInfinity, 
         double.PositiveInfinity);
    tb.Measure(maxSize);
    return tb.DesiredSize;
}

public static Testit() 
{
    TextBlock textBlock = new TextBlock();
    textBlock.Text = "NR valve";

    Size text size = ShapeMeasure(textBlock);
}
呆头 2024-09-11 11:40:01

由于 Measure 是在 UIElement 中定义的,因此您可以测量任何 UIElement 派生对象(包括 TextBlock)的形状:

public static Size ShapeMeasure(UIElement e) {
// Measured Size is bounded to be less than maxSize
Size maxSize = new Size(
     double.PositiveInfinity, 
     double.PositiveInfinity);
e.Measure(maxSize);
return e.DesiredSize;
}

And since Measure is defined in UIElement you can measure the shape of any UIElement derived object including TextBlock:

public static Size ShapeMeasure(UIElement e) {
// Measured Size is bounded to be less than maxSize
Size maxSize = new Size(
     double.PositiveInfinity, 
     double.PositiveInfinity);
e.Measure(maxSize);
return e.DesiredSize;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文