简体   繁体   English

C#WPF RichtextBox类型的行为更改

[英]C# WPF RichtextBox type behavior change

I have recently converted a TextBox (which I am using as a search box) to a RichTextBox in order to bold some contained words within the box. 我最近将一个TextBox(我正在用作搜索框)转换为RichTextBox,以加粗框中包含的某些单词。 Upon doing so I have noticed a few "errors". 这样做后,我注意到了一些“错误”。 I would like to know if they are intentional, and how to over-ride them. 我想知道它们是否是故意的,以及如何超越它们。

The first, and most important problem occurs only where I call a SelectAll() on the box to pull its information on the keyDown event: 第一个也是最重要的问题仅在我在框上调用SelectAll()以在keyDown事件上获取其信息时发生:

((RichTextBox)sender).SelectAll();
string search = ((RichTextBox)sender).Selection.Text;

This prevents me from writing more than a character in the RTB. 这使我无法在RTB中写更多的字符。 I presume that it is leaving my text selected and overwriting it with every key press. 我假设它是使我的文本处于选中状态,并且每次按键都将其覆盖。 I cannot find any DeSelect() method, so is there another means of attaining this string data to prevent this issue. 我找不到任何DeSelect()方法,因此还有另一种获取此字符串数据的方法来防止此问题。 I need the string to pass to a regular expression. 我需要将字符串传递给正则表达式。

Secondly, the RTB only throws the IBeam into the box on a double click. 其次,RTB只需双击即可将IBeam放入框中。 Is there a way to recreate the behavior of a TextBox that enters on a single click (or should it be entering on a single click by default?) 有没有一种方法可以重新创建单击一次即可输入的TextBox的行为(或者默认情况下是否应单击一次即可输入?)

Instead of selecting all, get the content and convert the flow document to plain text. 而不是全部选择内容,而是将流文档转换为纯文本。

FlowDocument doc =((RichTextBox)sender).Document;
string search = new TextRange(doc.ContentStart, doc.ContentEnd).Text;

Convert FlowDocument to simple text 将FlowDocument转换为简单文本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM