【问题标题】:Read-only Run elements in a WPF RichTextBox?WPF RichTextBox 中的只读运行元素?
【发布时间】:2010-11-08 05:48:52
【问题描述】:

我可能完全在想象这一点,但我可以发誓有一种方法可以使 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 似乎证实了这一点。

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

【问题讨论】:

    标签: wpf xaml richtextbox .net-3.5


    【解决方案1】:

    好的,我想出了一个适用于我的情况的解决方案 - 但可能不适用于所有想要这样的东西的人。它很乱,但它可以完成工作。

    几天之内我不会接受我自己的答案,以防万一其他人有更好的方法来完成此任务。

    我们开始吧,首先是 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 重新插入到当前插入符号位置即可。

    与任何 hack 一样,也有很多缺点。我发现的缺点如下:

    • 我想要只读的文本需要包含在InlineUIContainer 中。这对这个解决方案有点限制。
    • 我必须捕获“Enter”键并手动插入换行符,否则,InlineUIContainer.Unloaded() 会在每次按下 Enter 键时持续触发。不好玩,但它适用于我的情况。

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

    【讨论】:

    • 非常有趣,如果您愿意在文本编辑器中添加“小部件”,它将帮助我。我正在制作一个非常好的窗口,比如 Wiki 编辑器。因此,为了添加图像、类别和关键字,我使用了装饰富文本对象的小对象。我希望编辑器更像 Visual Studio 编辑器。这样我可以添加“装饰”但是......这绝对帮助了我!谢谢。
    • 很高兴它可以提供帮助——不过请注意——我隐约记得可以使用退格键删除只读文本。这段时间之后我不确定,但我会彻底测试它以确保小部件保持只读状态。
    【解决方案2】:

    这可以通过处理两个事件来实现: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>
    

    【讨论】:

    • 你忘记了手指触摸。
    猜你喜欢
    • 2015-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多