WPFToolkit RichTextBox - 在键入时更新文本颜色
我使用 WPFToolkit RichTextBox 来允许用户查看和编辑文本。 我需要支持的事情之一是特定单词在输入时以及从预先存在的源加载它们时的着色。
例如,如果用户键入单词“Hello”,它应该为蓝色。
因为我使用 MVVM,所以我想尽可能避免代码隐藏。
到目前为止,我一直在尝试使用自定义 TextFormatter 在 ViewModel 中处理此问题,但是仅当我更新源属性时它才有帮助,并且当我直接更新 RichTextBox 时它没有任何颜色,因为 TextFormatter 的 SetText 甚至没有被调用。仅调用了 SetText。
我希望你们能帮助我。
谢谢!
I'm using the WPFToolkit RichTextBox in order to allow the user to view and edit text.
One of the things I need to support is the coloring of specific words as they are being typed, as well as when loading them from an pre-existing source.
For example if the user type the word - "Hello" it should be colored in blue.
Since I'm using MVVM I would like to avoid code behind as much as possible.
So far I've been trying handling this in the ViewModel with a custom TextFormatter however it helped only when the I update the source property and when I updated the RichTextBox directly it didn't color anything as the TextFormatter's SetText wasn't even called. Only SetText was called.
I hope you guys could help me out.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后我使用了我在这里找到的解决方案 -
http://blogs.microsoft .co.il/blogs/tamir/archive/2006/12/14/RichTextBox-syntax-highlighting.aspx
它正在完成其工作,但它有时在快速打字或按住退格键时会出现轻微的卡顿。
所以现在它已经足够好了,因为我的 RichTextBox 相对较小。
不过,如果有人知道如何解决打嗝,那就太好了!
编辑:我找到了一种真正改善打嗝的方法。我使用了 DispatcherTimer 并在 TextChanged 事件上启动它,这样只有一秒/半秒后,它才会启动着色逻辑。到目前为止,它的运行速度要快得多,而且我在快速打字时没有注意到任何问题。因此,对于我对带有彩色单词的单行的需求,它似乎有效!
In the end I used the solution I found in here -
http://blogs.microsoft.co.il/blogs/tamir/archive/2006/12/14/RichTextBox-syntax-highlighting.aspx
It's doing its job, but it sometimes has minor hiccups when typing fast or when holding the backspace.
So for now its good enough since my RichTextBox is relatively small.
Though if anyone knows how to fix the hiccups, it would be really great!
EDIT: I found a way to really improve the hiccups. I used a DispatcherTimer and started it on the TextChanged event, that way only after a second/half a second, it would start the coloring logic. So far it works much faster, and I didn't notice any problems when typing quickly. So for my needs of a single row with colored words, it seems like it worked!