只读在WPF RichTextBox中运行元素?

时间:2009-07-05 17:40:56

标签: wpf xaml richtextbox .net-3.5

我可能完全想象这一点,但我可以发誓有一种方法可以将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>

现在,几周后,我尝试在RichTextBox中将Run元素设置为只读,但发现它似乎不可能。

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

我完全想象这个吗?或者有办法做我想做的事吗?

2 个答案:

答案 0 :(得分: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;
            }
        }
    }
}

我的解决方案依赖于以下事实:当从UI中删除InlineUIContainer时,会调用Unloaded()方法。此时,我只是将删除的InlineUIContainer重新插入当前的插入位置。

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

  • 我想要只读的文字需要包含在InlineUIContainer中。这对此解决方案来说有点限制。
  • 我必须捕获'Enter'键并手动插入换行符,否则每次按下Enter键时InlineUIContainer.Unloaded()都会一直触发。不好玩,但它适合我的情况。

这不是一个很好的解决方案,但我认为它对我有用。就像我说的那样,我不打算将此标记为我自己的问题的答案 - 希望其他人有更好的方法来做到这一点。

答案 1 :(得分: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>