[英]WPF TextBoxBase (System.Windows.Controls.Primitives) get text
How can I get the .Text
from a WPF (System.Windows.Controls.Primitives) TextBoxBase. 如何从WPF(System.Windows.Controls.Primitives)TextBoxBase获取
.Text
。 Here is the code: 这是代码:
private TextBoxBase mTextBox;
this.mTextBox.Text;
The WPF Controls does not contain a definitions for .Text
I also tried using a TextRange but that did not work. WPF控件不包含
.Text
的定义我也尝试使用TextRange,但是没有用。 Here is the code: 这是代码:
string other = new TextRange(((RichTextBox)sender).Document.ContentStart, ((RichTextBox)sender).Document.ContentEnd).Text;
How can I get the .Text
from my WPF (System.Windows.Controls.Primitives) TextBoxBase? 如何从我的WPF(System.Windows.Controls.Primitives)TextBoxBase中获取
.Text
?
There are not any Text property in the WPF RichTextBox
control. WPF
RichTextBox
控件中没有任何Text属性。 Below is a way to get all texts: 以下是获取所有文本的方法:
string GetString(RichTextBox rtb)
{
var textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
return textRange.Text;
}
If you question stays at 如果你质疑留下来
How can I get the .Text from my WPF (System.Windows.Controls.Primitives)
TextBoxBase
?如何从我的WPF(System.Windows.Controls.Primitives)
TextBoxBase
获取.Text?
Then the short answer is: you can't since there is no such property. 然后简短的回答是:你不能,因为没有这样的财产。
Now the only known controls that directly inherit from TextBoxBase
are TextBox
and RichTextBox
. 现在,直接从
TextBoxBase
继承的唯一已知控件是TextBox
和RichTextBox
。 TextBox
DOES have a Text property, but RichTextBox
DOESN'T. TextBox
具有Text属性,但RichTextBox
不具有。
If you are using TextBox, you can cast your object and then get the property: 如果您使用的是TextBox,则可以转换对象,然后获取属性:
var textBox = mTextBox as TextBox;
if (textBox != null)
{
var text = textBox.Text;
}
Maybe use an extension method like this? 也许使用像这样的扩展方法?
public static string GetTextValue(this TextBoxBase source)
{
// need to cast TextBoxBase to one of its implementations
var txtControl = source as TextBox;
if (txtControl == null)
{
var txtControlRich = source as RichTextBox;
if (txtControlRich == null) return null;
return txtControlRich.Text;
}
return txtControl.Text;
}
I haven't tested this code, but hopefully you get the general idea. 我没有测试过这段代码,但希望你能得到一般的想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.