C# WPF TextBoxBase(System.Windows.Controls.Primitives)获取文本
如何从WPF(System.Windows.Controls.Primitives)TextBoxBase获取C# WPF TextBoxBase(System.Windows.Controls.Primitives)获取文本,c#,wpf,C#,Wpf,如何从WPF(System.Windows.Controls.Primitives)TextBoxBase获取.Text。代码如下: private TextBoxBase mTextBox; this.mTextBox.Text; string other = new TextRange(((RichTextBox)sender).Document.ContentStart, ((RichTextBox)sender).Document.ContentEnd).Tex
.Text
。代码如下:
private TextBoxBase mTextBox;
this.mTextBox.Text;
string other = new TextRange(((RichTextBox)sender).Document.ContentStart, ((RichTextBox)sender).Document.ContentEnd).Text;
WPF控件不包含的定义。Text
我也尝试过使用TextRange,但不起作用。代码如下:
private TextBoxBase mTextBox;
this.mTextBox.Text;
string other = new TextRange(((RichTextBox)sender).Document.ContentStart, ((RichTextBox)sender).Document.ContentEnd).Text;
如何从WPF(System.Windows.Controls.Primitives)TextBoxBase中获取
.Text
WPFRichTextBox
控件中没有任何文本属性。以下是获取所有文本的方法:
string GetString(RichTextBox rtb)
{
var textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
return textRange.Text;
}
如果你的问题停留在 如何从WPF(System.Windows.Controls.Primitives)中获取.Text 那么简单的回答是:你不能,因为没有这样的财产 现在唯一已知的直接从继承的控件是和。确实有财产,但没有 如果使用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;
}
我还没有测试过这段代码,但希望您能了解大致情况。更多信息,请查看此否,这是RichTextBox的代码我需要TextBoxBase的代码如果您使用的是
RichTextBox
,为什么需要TextBoxBase
的代码?Text
属性仅存在于TextBox
类中TextBoxBase
是这两个控件的基类,但它不包含这样的属性TextBoxBase
无论如何都是一个抽象类,因此不能实例化它。请澄清您试图实现的目标。我相信RichTextBox
是发件人,但我使用的是TextBoxBase
。TextBoxBase
如果用于RichTextBox
?既然是抽象类型,您怎么能使用TextBoxBase
?您的对象应该有一个更具体的类型:TextBox
或RichTextBox
(或更多派生类型),否则这是不可能的。还向我们展示XAML文档。