WPF RichTextBox 中的只读 Run 元素?
我可能完全是在想象这一点,但我可以发誓有一种方法可以使 RichTextBox 中的各个 Run(或 Parapgraph)元素成为只读。 我还可以发誓,几周前我自己尝试了一种方法,并对结果感到满意 - 我隐约记得它看起来像这样:
<RichTextBox x:Name="richTextBox"
AcceptsTab="True"
AcceptsReturn="True"
FontFamily="Courier New"
FontSize="14">
<FlowDocument>
<Paragraph>
<Run IsReadOnly="True">I wish this was read-only!</Run>
</Paragraph>
</FlowDocument>
</RichTextBox>
现在,几周后,我尝试让 Run 元素读取-只有在RichTextBox中才发现它似乎不可能。
这是我完全想象出来的吗? 或者有办法做我想做的事吗?
I may be completely imagining this, but I could have sworn there was a way to make individual Run (or Parapgraph) elements in a RichTextBox read-only. I also could have sworn I tried a method for doing this out myself a few weeks ago and was satisfied with the results - I vaguely remember it looked something like this:
<RichTextBox x:Name="richTextBox"
AcceptsTab="True"
AcceptsReturn="True"
FontFamily="Courier New"
FontSize="14">
<FlowDocument>
<Paragraph>
<Run IsReadOnly="True">I wish this was read-only!</Run>
</Paragraph>
</FlowDocument>
</RichTextBox>
Now, a few weeks later, I go to try to make Run elements read-only in a RichTextBox only to find it doesn't seem to be possible.
This post on the MSDN forums seems to confirm that.
Did I completely imagine this? Or is there a way to do what I want to do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我已经想出了一个适合我的情况的解决方案 - 但可能不适用于每个想要这样的东西的人。 虽然很乱,但它能完成工作。
几天内我不会接受我自己的答案,以防万一其他人有更好的方法来完成这个任务。
首先是 XAML:
以及背后的代码:
我的解决方案依赖于这样一个事实:当从 UI 中删除
InlineUIContainer
时,它的Unloaded()
方法是叫。 此时,我只需将已删除的InlineUIContainer
重新插入到当前插入符号位置即可。与任何黑客一样,也有很多缺点。 我发现的缺点如下:
InlineUIContainer
中。 这对该解决方案有一点限制。InlineUIContainer.Unloaded()
都会不断触发。 不好玩,但它适用于我的情况。这不是一个很好的解决方案,但我认为它对我有用。 就像我说的,我不会将此标记为我自己问题的答案 - 希望其他人有更好的方法来做到这一点。
Alright, I've come up with a solution that works for my case - but may not work for everyone who wants something like this. It's messy, but it does the job.
I'm not going to accept my own answer for a few days, just in case someone else has a better way of accomplishing this.
Here we go, first, the XAML:
And the code behind:
My solution relies on the fact that when an
InlineUIContainer
is removed from the UI, it'sUnloaded()
method is called. At that point, I simply reinsert the deletedInlineUIContainer
at the current caret position.As with any hack, there are a bunch of disadvantages. The disadvantages I'm finding are the following:
InlineUIContainer
. That is a little limiting for this solution.InlineUIContainer.Unloaded()
keeps firing everytime the Enter key is pressed. Not fun, but it works for my case.It's not a great solution, but I think it will work for me. Like I said, I'm not going to mark this as an answer to my own question yet - hopefully someone else will have a better way of doing this.
这可以通过处理两个事件来实现:1) OnMouseDown 2) OnPreviewKeyDown
在 Xaml 中,我们必须按以下方式处理它:
This can be achieved by handling two event: 1) OnMouseDown 2) OnPreviewKeyDown
IN Xaml we have to handle it in following way: