如何防止将某些类型的格式粘贴到 WPF RichTextBox 中

发布于 2024-10-18 15:18:04 字数 270 浏览 5 评论 0原文

我想在 WPF RichTextBox 中允许一些简单的格式化命令,但不允许其他命令。

我创建了一个工具栏,允许用户应用粗体或斜体,并使用项目符号或编号列表。 (基本上,我只想支持适合博客或 wiki 的格式化命令。)

问题是用户可以执行剪切和粘贴操作,插入具有前景色和背景色的文本,以及其他类型的不允许的格式。这可能会导致严重的可用性问题,例如用户将白色文本粘贴到白色背景上。

有什么办法可以关闭这些高级格式化功能吗?如果没有,有没有办法可以拦截粘贴操作并删除我不想要的格式?

I want to allow some simple formatting commands within a WPF RichTextBox but not others.

I've created a toolbar that allows users to apply bold or italics, and use bulleted or numbered lists. (Basically, I only want to support the formatting commands that would be appropriate for a blog or wiki.)

The problem is that users can perform cut and paste operations that insert text with foreground and background colors, among other kinds of disallowed formatting. This can lead to nasty usability issues like users pasting white text onto a white background.

Is there any way to turn these advanced formatting features off? If not, is there a way I can intercept the paste operation and strip out the formatting I don't want?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

装迷糊 2024-10-25 15:18:04

您可以像这样拦截粘贴操作:

    void AddPasteHandler()
    {
        DataObject.AddPastingHandler(richTextBox, new DataObjectPastingEventHandler(OnPaste));
    }

    void OnPaste(object sender, DataObjectPastingEventArgs e)
    {
        if (!e.SourceDataObject.GetDataPresent(DataFormats.Rtf, true)) return;
        var rtf = e.SourceDataObject.GetData(DataFormats.Rtf) as string;
        // Change e.SourceDataObject to strip non-basic formatting...
    }

混乱的部分是保留一些但不是全部格式。 rtf 变量将是 RTF 格式的字符串,您可以使用第三方库来解析它,使用类似 DOM 的模式遍历树,并发出仅包含文本、粗体和斜体的新 RTF 。然后将其塞回到 e.SourceDataObject 或许多其他选项中(请参阅下面的文档)。

以下是 PastingHandler 文档:

这是众多 RTF 解析器之一:

You can intercept the paste operation like this:

    void AddPasteHandler()
    {
        DataObject.AddPastingHandler(richTextBox, new DataObjectPastingEventHandler(OnPaste));
    }

    void OnPaste(object sender, DataObjectPastingEventArgs e)
    {
        if (!e.SourceDataObject.GetDataPresent(DataFormats.Rtf, true)) return;
        var rtf = e.SourceDataObject.GetData(DataFormats.Rtf) as string;
        // Change e.SourceDataObject to strip non-basic formatting...
    }

and the messy part is keeping some but not all of the formatting. The rtf variable will be a string in RTF format that you can use a third-party libary to parse, walk the tree using a DOM-like pattern, and emit new RTF with just text, bold and italics. Then cram that back into e.SourceDataObject or a number of other options (see the docs below).

Here are PastingHandler docs:

Here is one of many RTF parsers:

吃→可爱长大的 2024-10-25 15:18:04

如果您想从粘贴的内容中删除所有格式,请使用以下代码(不是您所要求的,但可能对某人有用):

    void OnPaste(object sender, DataObjectPastingEventArgs e)
    {
        if (!e.SourceDataObject.GetDataPresent(DataFormats.Rtf, true)) return;
        var rtf = e.SourceDataObject.GetData(DataFormats.Rtf) as string;

        FlowDocument document = new FlowDocument();
        document.SetValue(FlowDocument.TextAlignmentProperty, TextAlignment.Left);

        TextRange content = new TextRange(document.ContentStart, document.ContentEnd);

        if (content.CanLoad(DataFormats.Rtf) && string.IsNullOrEmpty(rtf) == false)
        {
            // If so then load it with RTF
            byte[] valueArray = Encoding.ASCII.GetBytes(rtf);
            using (MemoryStream stream = new MemoryStream(valueArray))
            {
                content.Load(stream, DataFormats.Rtf);
            }
        }

        DataObject d = new DataObject();
        d.SetData(DataFormats.Text, content.Text.Replace(Environment.NewLine, "\n"));
        e.DataObject = d;
    }
}

Here is the code if you wanted to strip all formatting from pasted content (Not what you asked, but may be usefull to someone):

    void OnPaste(object sender, DataObjectPastingEventArgs e)
    {
        if (!e.SourceDataObject.GetDataPresent(DataFormats.Rtf, true)) return;
        var rtf = e.SourceDataObject.GetData(DataFormats.Rtf) as string;

        FlowDocument document = new FlowDocument();
        document.SetValue(FlowDocument.TextAlignmentProperty, TextAlignment.Left);

        TextRange content = new TextRange(document.ContentStart, document.ContentEnd);

        if (content.CanLoad(DataFormats.Rtf) && string.IsNullOrEmpty(rtf) == false)
        {
            // If so then load it with RTF
            byte[] valueArray = Encoding.ASCII.GetBytes(rtf);
            using (MemoryStream stream = new MemoryStream(valueArray))
            {
                content.Load(stream, DataFormats.Rtf);
            }
        }

        DataObject d = new DataObject();
        d.SetData(DataFormats.Text, content.Text.Replace(Environment.NewLine, "\n"));
        e.DataObject = d;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文