如何复制 WPF FlowDocument InlineUIContainer 内容?

发布于 2024-08-09 05:33:43 字数 310 浏览 8 评论 0原文

我有一个 WPF FlowDocument,其中有一些 InlineUIContainer,这些是简单的 InlineUIContainer,其中包含一个样式化按钮,其中 Button.Content 中包含一些文本。当我将包含按钮的文本和 InlineUIContainer 从 FlowDocument 复制到 TextBox 时,不会复制该按钮。

可以以某种方式转换内联按钮或将按钮转换为粘贴的文本数据中的文本。我尝试过使用 FlowDocument.DataObject.Copying 事件,但我似乎找不到任何关于如何使用它的好示例,或者即使这是正确的方向。

谢谢

I have a WPF FlowDocument that has a few InlineUIContainers, these are simple InlineUIContainers, that contain a styled button with some text in the Button.Content. When I copy the text and InlineUIContainer containing the button from the FlowDocument to a TextBox, the button is not copied.

It is possible to somehow convert the inline button or convert the button to text in the pasted text data. I have tried using the FlowDocument.DataObject.Copying event, but I can't seem to find any good samples on how to use this or even if this is the right direction.

Thank you

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

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

发布评论

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

评论(1

夜唯美灬不弃 2024-08-16 05:33:43

我遇到了同样的问题,并设法得到类似以下内容的工作:

public class MyRichTextBox : RichTextBox
{
    public MyRichTextBox()
        : base()
    {
        CommandManager.RegisterClassCommandBinding(typeof(MyRichTextBox),
                                                   new CommandBinding(ApplicationCommands.Copy, OnCopy, OnCanExecuteCopy));
    }

    private static void OnCanExecuteCopy(object target, CanExecuteRoutedEventArgs args)
    {
        MyRichTextBox myRichTextBox = (MyRichTextBox)target;
        args.CanExecute = myRichTextBox.IsEnabled && !myRichTextBox.Selection.IsEmpty;
    }

    private static void OnCopy(object sender, ExecutedRoutedEventArgs e)
    {
        MyRichTextBox myRichTextBox = (MyRichTextBox)sender;
        Clipboard.SetText(GetInlineText(myRichTextBox));
        e.Handled = true;
    }

    private static string GetInlineText(RichTextBox myRichTextBox)
    {
        StringBuilder sb = new StringBuilder();
        foreach (Block b in myRichTextBox.Document.Blocks)
        {
            if (b is Paragraph)
            {
                foreach (Inline inline in ((Paragraph)b).Inlines)
                {
                    if (inline is InlineUIContainer)
                    {
                        InlineUIContainer uiContainer = (InlineUIContainer)inline;
                        if (uiContainer.Child is Button)
                            sb.Append(((Button)uiContainer.Child).Content);
                    }
                    else if (inline is Run)
                    {
                        Run run = (Run)inline;
                        sb.Append(run.Text);
                    }
                }
            }
        }
        return sb.ToString();
    }
}

当然,这非常简单 - 您可能会创建 Button 的子类并定义一个像“GetCopyToClipboardText”这样的接口函数,而不是“如何获取文本”来自按钮”-richtextbox 内的代码。

该示例复制了 richtextbox 内的所有文本 - 如果仅将文本框的选定部分复制到剪贴板,则会更有用。 这篇文章给出了如何实现这一目标的示例。

I had the same problem and managed to get something like the following to work:

public class MyRichTextBox : RichTextBox
{
    public MyRichTextBox()
        : base()
    {
        CommandManager.RegisterClassCommandBinding(typeof(MyRichTextBox),
                                                   new CommandBinding(ApplicationCommands.Copy, OnCopy, OnCanExecuteCopy));
    }

    private static void OnCanExecuteCopy(object target, CanExecuteRoutedEventArgs args)
    {
        MyRichTextBox myRichTextBox = (MyRichTextBox)target;
        args.CanExecute = myRichTextBox.IsEnabled && !myRichTextBox.Selection.IsEmpty;
    }

    private static void OnCopy(object sender, ExecutedRoutedEventArgs e)
    {
        MyRichTextBox myRichTextBox = (MyRichTextBox)sender;
        Clipboard.SetText(GetInlineText(myRichTextBox));
        e.Handled = true;
    }

    private static string GetInlineText(RichTextBox myRichTextBox)
    {
        StringBuilder sb = new StringBuilder();
        foreach (Block b in myRichTextBox.Document.Blocks)
        {
            if (b is Paragraph)
            {
                foreach (Inline inline in ((Paragraph)b).Inlines)
                {
                    if (inline is InlineUIContainer)
                    {
                        InlineUIContainer uiContainer = (InlineUIContainer)inline;
                        if (uiContainer.Child is Button)
                            sb.Append(((Button)uiContainer.Child).Content);
                    }
                    else if (inline is Run)
                    {
                        Run run = (Run)inline;
                        sb.Append(run.Text);
                    }
                }
            }
        }
        return sb.ToString();
    }
}

Of course this is very simplistic - you would probably create a subclass of Button and define an interface-function like "GetCopyToClipboardText" instead of having the "how to get text from a button"-code inside the richtextbox.

The example copies all text inside the richtextbox - it would be more usefull if only the selected part of the textbox was copied to the clipboard. This post gives an example of how to achive that.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文