只读运行在一个WPF RichTextBox的内容是什么? [英] Read-only Run elements in a WPF RichTextBox?
问题描述
我可以完全想象这一点,但我可以发誓有一种方法,使各个运行(或Parapgraph)元素在一个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>
现在,几个星期后,我去试试,使运行元素只读一个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.
<一个href="http://social.msdn.microsoft.com/forums/en-US/wpf/thread/329079fc-8555-4192-b90e-6fccd43bf28a">This张贴在MSDN论坛似乎证实了这一点。
难道我完全想象吗?或者是有没有办法做我想做的事?
Did I completely imagine this? Or is there a way to do what I want to do?
推荐答案
好吧,我想出了一个解决方案,适用于我的情况 - 但是可能大家谁愿意这样的事情不能正常工作。它的混乱,但它的工作。
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.
在这里,我们去,首先,XAML:
Here we go, first, the XAML:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1"
Height="500"
Width="600">
<DockPanel LastChildFill="True">
<RichTextBox x:Name="rtb"
FontFamily="Courier New"
FontSize="14"
PreviewKeyDown="rtb_PreviewKeyDown">
<FlowDocument>
<Paragraph>
<InlineUIContainer Unloaded="InlineUIContainer_Unloaded">
<TextBlock FontFamily="Courier New" FontSize="14">This line of text is not editable.</TextBlock>
</InlineUIContainer>
<Run Foreground="Blue">But this is editable.</Run>
</Paragraph>
</FlowDocument>
</RichTextBox>
</DockPanel>
</Window>
和后面的code:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
namespace WpfApplication1
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void InlineUIContainer_Unloaded(object sender, RoutedEventArgs e)
{
(sender as InlineUIContainer).Unloaded -= new RoutedEventHandler(InlineUIContainer_Unloaded);
TextBlock tb = new TextBlock();
tb.FontFamily = new FontFamily("Courier New");
tb.FontSize = 14;
tb.Text = "This line of text is not editable.";
TextPointer tp = rtb.CaretPosition.GetInsertionPosition(LogicalDirection.Forward);
InlineUIContainer iuic = new InlineUIContainer(tb, tp);
iuic.Unloaded += new RoutedEventHandler(InlineUIContainer_Unloaded);
}
private void rtb_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
var newPointer = rtb.Selection.Start.InsertLineBreak();
rtb.Selection.Select(newPointer, newPointer);
e.Handled = true;
}
}
}
}
我的解决方案依赖于一个事实,当一个 InlineUIContainer
从UI中删除,这是卸载()
方法被调用。在这一点上,我只是重新插入删除 InlineUIContainer
在当前插入符号位置。
My solution relies on the fact that when an InlineUIContainer
is removed from the UI, it's Unloaded()
method is called. At that point, I simply reinsert the deleted InlineUIContainer
at the current caret position.
对于任何黑客,有一堆缺点。是我发现的缺点如下:
As with any hack, there are a bunch of disadvantages. The disadvantages I'm finding are the following:
- 我要为只读需要的文本被圈在一个
InlineUIContainer
。这是一个有点限制了该解决方案。 - 我要捕捉Enter键,并插入换行符手动,否则,
InlineUIContainer.Unloaded()
保持射击每次回车键pressed。不好玩,但它适合我的情况。
- The text I want to be read-only needs to be wrapped in a
InlineUIContainer
. That is a little limiting for this solution. - I have to capture the 'Enter' key and insert line breaks manually, otherwise,
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.
这篇关于只读运行在一个WPF RichTextBox的内容是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!