简体   繁体   English

WPF TextBoxBase(System.Windows.Controls.Primitives)获取文本

[英]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继承的唯一已知控件是TextBoxRichTextBox 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.

相关问题 无法分配给属性“System.Windows.Controls.Primitives.ButtonBase.Click” - Failed to assign to property 'System.Windows.Controls.Primitives.ButtonBase.Click' System.Windows.Forms.RichTextBox的TextBoxBase.ClearUndo()实现 - System.Windows.Forms.RichTextBox's implementation of TextBoxBase.ClearUndo() NullReferenceException与System.Windows.Controls.WebBrowser WPF - NullReferenceException with System.Windows.Controls.WebBrowser WPF 设置属性“System.Windows.Controls.Primitives.ToggleButton.IsChecked”引发异常 - Set property 'System.Windows.Controls.Primitives.ToggleButton.IsChecked' threw an exception 如何获取非绑定WPF组合框以显示选定的项目而不是{System.Windows.Controls.ComboBoxItem: - How to get non-bound WPF combobox to show the selecteditem instead of {System.Windows.Controls.ComboBoxItem: 无法从文本'鼠标'创建'Windows.UI.Xaml.Controls.Primitives.PlacementMode' - Failed to create a 'Windows.UI.Xaml.Controls.Primitives.PlacementMode' from the text '鼠标' 如何将“System.Windows.Controls.TextBlock”转换为“System.Windows.Controls.Control”WPF3DFD74EFA19FBEZA23 - How to convert 'System.Windows.Controls.TextBlock' to 'System.Windows.Controls.Control' WPF C# 将 wpf xaml 图像绑定到 System.Windows.Controls.Image 不起作用 - Binding a wpf xaml image to System.Windows.Controls.Image is not working 继续收到此错误:事件'System.Windows.Controls.Primitives.ToggleButton.Checked'只能出现在+ =或 - =的左侧 - Keep getting this error: The event 'System.Windows.Controls.Primitives.ToggleButton.Checked' can only appear on the left hand side of += or -= WPF - 从另一个线程更新“System.Windows.Controls.Image” - WPF - Update “System.Windows.Controls.Image” from another thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM