简体   繁体   English

更改TextBox的字体大小

[英]Change font size of a TextBox

in WPF It it possible to change a text box's font size during runtime? 在WPF中可以在运行时更改文本框的字体大小吗?

i tried to do that: 我试着这样做:

foreach (Control ctrl in gridArray[i].Children)
 {
    if(ctrl.GetType() == typeof(TextBox))
    {
        (TextBox)ctrl.FontSize = (double)5;

    }
 }

but it didnt work 但它没有用

The cast does not have a high precedence, your code effectively tries to cast the value in ctrl.FontSize to TextBox , you need to add parenthesis (and the double cast is superfluous): ctrl.FontSize转换没有高优先级,你的代码有效地尝试将ctrl.FontSize的值ctrl.FontSizeTextBox ,你需要添加括号(并且双重转换是多余的):

((TextBox)ctrl).FontSize = 5;

Further the way you check the type of the control is not such a good idea, use is instead. 另外您检查控件的类型的方法是不是一个好主意,用is吧。 Otherwise sublasses of TextBox are not included. 否则不包括TextBox类。

if (ctrl is TextBox)

Further as you do not only care about the type and cast as well to interact with the TextBox class interface you may as well use as : 另外,你不仅关心类型和投以及与交互TextBox类的接口你不妨使用as

var textBox = ctrl as TextBox;
if (textBox != null)
    textBox.FontSize = 5;

This also conveniently gets rid of the parenthesis jungle. 这也方便地摆脱了括号丛林。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM