如何计算WPF TextBlock的宽度其已知的字体大小和字符? [英] How to calculate WPF TextBlock width for its known font size and characters?

查看:419
本文介绍了如何计算WPF TextBlock的宽度其已知的字体大小和字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有的TextBlock 文本一些文本字体大小10.0

我如何计算相应的的TextBlock 宽度

推荐答案

使用的<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.media.formattedtext.aspx\"><$c$c>FormattedText类。

我在code做了一个辅助函数:

I made a helper function in my code:

private Size MeasureString(string candidate)
{
    var formattedText = new FormattedText(
        candidate,
        CultureInfo.CurrentUICulture,
        FlowDirection.LeftToRight,
        new Typeface(this.textBlock.FontFamily, this.textBlock.FontStyle, this.textBlock.FontWeight, this.textBlock.FontStretch),
        this.textBlock.FontSize,
        Brushes.Black);

    return new Size(formattedText.Width, formattedText.Height);
}

这篇关于如何计算WPF TextBlock的宽度其已知的字体大小和字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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