设置WPF文本框控件的Text属性时,其他属性也应该更改(作为副作用)不会更改。特别是,我想在设置Text之后检查ExtentWidth属性的值,但它不会改变。我试过调用UpdateLayout()无济于事。在Windows.Forms中,我会调用DoEvents()。设置文本后更新WPF文本框属性
好的,这是一些代码。我把它放在Window_Loaded()事件处理程序中。问题是textBox.ExtentWidth在textBox.Text更改时不会更改。这并不令我感到惊讶。我想我需要调用像textBox.UpdateLayout(),以重新计算ExtentWidth,但没有帮助。 ExtentWidth确实取决于我在Window的构造函数中初始化textBox.Text的内容,但这对我没有帮助。我需要设置几个不同的文本值,并获得相应的每个ExtentWidth。
string initText = textBox.Text; // "textBox"
double extentWidth = textBox.ExtentWidth; // 39.3
textBox.Text = "short text";
extentWidth = textBox.ExtentWidth; // 39.3
textBox.Text = "Long enough to make a difference, eh?";
extentWidth = textBox.ExtentWidth; // 39.3
你的价值是多少,是0.0? – Maheep 2011-12-16 04:39:54