【问题标题】:Finding the maximum possible font size for multiple TextBlocks查找多个 TextBlock 的最大可能字体大小
【发布时间】:2012-09-18 10:09:18
【问题描述】:

我在固定大小列Grid 中有几个TextBlocks。即Grid 的大小可能会改变,但它的列宽都是相同的(例如,整个大小的 10%)并且每列都包含一个 TextBlock

所有TextBlocks的字体大小必须相同。

如何找到使TextBlocks 中的所有文本可见的最大可能字体大小?

【问题讨论】:

    标签: c# wpf windows-8 microsoft-metro


    【解决方案1】:

    您可以使用Graphics.MeasureString Method (String, Font, Int32) 来计算字体大小。 然后使用一些行为魔术将字体大小绑定到 TextBlock。

    private void MeasureStringWidth(PaintEventArgs e)
    {
    
        // Set up string. 
        string measureString = "Measure String";
        Font stringFont = new Font("Arial", 16);
    
        // Set maximum width of string. 
        int stringWidth = 200;
    
        // Measure string.
        SizeF stringSize = new SizeF();
        stringSize = e.Graphics.MeasureString(measureString, stringFont, stringWidth);
    
        // Draw rectangle representing size of string.
        e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 0.0F, 0.0F, stringSize.Width, stringSize.Height);
    
        // Draw string to screen.
        e.Graphics.DrawString(measureString, stringFont, Brushes.Black, new PointF(0, 0));
    }
    

    【讨论】:

      猜你喜欢
      • 2011-11-17
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 2011-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多