【问题标题】:Setting up font of TextBox from code behind从后面的代码设置 TextBox 的字体
【发布时间】:2011-04-29 11:21:27
【问题描述】:

如何在后面的代码中从string 设置TextBox 的字体?

// example
txtEditor.FontFamily = "Consolas";

【问题讨论】:

  • 不是一个愚蠢的问题。

标签: c# wpf code-behind


【解决方案1】:
txtEditor.FontFamily = new FontFamily("Consolas"); // the Media namespace

【讨论】:

【解决方案2】:

使用以下语法:

lblCounting.Font  = new Font("Times New Roman", 50);

lblCounting 是任何标签。

【讨论】:

  • 使用降价和格式化工具让你的代码显示为代码:lblCounting.Font = new Font("Times New Roman", 50);
【解决方案3】:
System.Drawing.Font = new Font("Arial", 8, FontStyle.Bold);

【讨论】:

  • 问题询问如何设置字体,但示例暗示 OP 想要设置 FontFamily。如果不是示例,则此答案回答了问题,并且是我正在寻找的答案(尽管我需要对其进行一些更正。)
  • 可以肯定的是,OP在问题中没有提到FontFamily,但你是对的,它在示例中,但仅适用于TextBox,而不是在程序范围内设置Font。跨度>
【解决方案4】:

将您的示例代码复制并粘贴到表单的构造函数中,紧跟在InitializeComponent(); 之后

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        txtEditor.FontFamily = new FontFamily("Consolas");
    }
}

【讨论】:

    【解决方案5】:

    一种以编程方式在全局范围内执行此操作的简单方法:

    public MainWindow()
    {
        this.FontFamily = new FontFamily("Segoe UI");
    }
    

    【讨论】:

      【解决方案6】:

      使用 txtEditor.Font.Name = "Consolas";

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-08
        • 2016-07-06
        • 1970-01-01
        • 1970-01-01
        • 2017-08-03
        • 2018-10-15
        • 2012-09-20
        • 1970-01-01
        相关资源
        最近更新 更多