WPF,计算绘制一段文字需要多少宽度

private double GetTextWidth(string text)
{
var textBlock = new System.Windows.Controls.TextBlock { Text = text, TextWrapping = TextWrapping.NoWrap, FontSize = 14 };

textBlock.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));

textBlock.Arrange(new Rect(textBlock.DesiredSize));
return textBlock.ActualWidth;
}

posted @ 2021-10-13 09:56  奇迹之耀  阅读(152)  评论(0编辑  收藏  举报