WPF TextBoxBase (System.Windows.Controls.Primitives)获取文本
本文关键字:Primitives 获取 取文本 Controls Windows TextBoxBase System WPF | 更新日期: 2023-09-27 17:52:59
如何从WPF (System.Windows.Controls.Primitives) TextBoxBase获得.Text
。下面是代码:
private TextBoxBase mTextBox;
this.mTextBox.Text;
WPF控件不包含.Text
的定义,我也尝试使用TextRange,但没有工作。下面是代码:
string other = new TextRange(((RichTextBox)sender).Document.ContentStart, ((RichTextBox)sender).Document.ContentEnd).Text;
我怎么能得到.Text
从我的WPF (System.Windows.Controls.Primitives) TextBoxBase?
WPF RichTextBox
控件中没有任何Text属性。下面是获取所有文本的方法:
string GetString(RichTextBox rtb)
{
var textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
return textRange.Text;
}
如果你的问题停留在
如何从我的WPF (System.Windows.Controls.Primitives)
TextBoxBase
中获得。text ?
那么简短的回答是:你不能,因为没有这样的属性。
现在唯一已知的直接从TextBoxBase
继承的控件是TextBox
和RichTextBox
。TextBox
有Text属性,但RichTextBox
没有。
如果你正在使用TextBox,你可以强制转换你的对象,然后获得属性:
var textBox = mTextBox as TextBox;
if (textBox != null)
{
var text = textBox.Text;
}
也许使用这样的扩展方法?
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;
}
我还没有测试这段代码,但希望你能得到一个大致的想法。