2009-07-05 35 views
11

我可能完全想象到这一点,但我可以发誓,有一种方法可以使RichTextBox中的单个Run(或Parapgraph)元素为只读。我还可以发誓我试过的方法在几个星期前做了这一点,我和很满意的结果 - 我依稀记得它看起来是这样的:只读在WPF RichTextBox中运行元素?

<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中的Run元素只读来发现它似乎不可能。

This post on the MSDN forums似乎证实了这一点。

我完全想象这个吗?还是有办法做我想做的事情?

回答

7

好吧,我想出了一个适用于我的案例的解决方案 - 但可能不适合每个想要类似这样的人。这很混乱,但是它完成了这项工作。

我不会接受我自己的答案几天,以防万一别人有更好的方式来完成这一点。

在这里,我们走了,首先,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> 

而后面的代码:

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从用户界面中删除,这是Unloaded()方法被调用。此时,我只需将删除的InlineUIContainer重新插入当前的插入位置即可。

与任何黑客一样,存在一系列的缺点。我发现的缺点如下:

  • 我希望为只读的文本需要用InlineUIContainer包装。这对这个解决方案有一点限制。
  • 我必须捕捉'Enter'键并手动插入换行符,否则,每次按下Enter键时,InlineUIContainer.Unloaded()都会持续发射。不好玩,但它适用于我的情况。

这不是一个很好的解决方案,但我认为它会适用于我。就像我说的那样,我不打算把这个标记作为我自己问题的答案 - 希望别人能有更好的方式来做到这一点。

+2

非常有趣的它会帮助我补充“小工具”,如果你将进入文本编辑器。我正在制作一个非常漂亮的窗口,比如Wiki编辑器。因此,为了添加图片,类别和关键字,我使用装饰富文本对象的小对象。我希望编辑更像是一个视觉工作室编辑。这样我可以添加“装饰物”,但是......这确实帮助了我!谢谢。 – JustinKaz 2010-12-30 19:47:19

+0

很高兴它可以提供帮助 - 但需要警告 - 我依稀记得可以使用退格键删除只读文本。这一次我不确定,但我会彻底测试它,以确保小部件保持只读状态。 – 2011-01-04 18:44:25

1

这可以通过处理两个事件来实现:1)onmousedown事件2)OnPreviewKeyDown

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows.Controls; 
using System.Windows; 
using System.Windows.Input; 

namespace WpfApplication2 
{ 
public class MultiPartTextBox : TextBox 
{ 
    private string _prefix; 
    private string _content; 

    public string Prefix 
    { 
     get { return _prefix; } 
     set { _prefix = value; 
     Text = _prefix; 
     } 
    } 

    public string Content 
    { 
     get { return _content; } 
     set { 
      _content = value; 
      Text = _prefix + _content; 
     } 
    } 

    public MultiPartTextBox() { _prefix = string.Empty; } 

    protected override void OnMouseDown(MouseButtonEventArgs e) 
    { 

     base.OnMouseDown(e); 
     this.SelectionStart = _prefix.Length; 
     this.SelectionLength = 0; 
    } 

    //tab In 
    protected override void OnGotFocus(RoutedEventArgs e) 
    { 
     this.SelectionStart = _prefix.Length; 
     this.SelectionLength = 0; 
     base.OnGotFocus(e); 
    } 

    protected override void OnPreviewKeyDown(KeyEventArgs e) 
    { 
     if (e.Key == Key.Back 
      || e.Key == Key.Delete 
      || e.Key==Key.Left) 
     { 
      if (CaretIndex <= _prefix.Length) 
      { 
       e.Handled = true; 
       return; 
      } 
     } 
     base.OnPreviewKeyDown(e); 
    } 
    } 
    } 

XAML中,我们必须处理它的方式如下:

xmlns:uc="clr-namespace:WpfApplication2" 

     <uc:MultiPartTextBox Height="30" HorizontalAlignment="Left" 
      Margin="80,94,0,0" x:Name="multiPartTxt1" VerticalAlignment="Top" 
      Width="224" Prefix="NON-EDITABLE" CaretIndex="4" >    
     </uc:MultiPartTextBox>