简体   繁体   English

WPF Richtextbox 图像和调整装饰器

[英]WPF Richtextbox images and resize adorners

Good morning, I have a richtextbox which allows me to insert an image via right click context menu.早上好,我有一个 richtextbox,它允许我通过右键单击上下文菜单插入图像。 This functionality works, and the image is inserted into the richtextbox with no issues (adorner shows).此功能有效,并且图像已毫无问题地插入到富文本框中(装饰器显示)。

Working adorner工作装饰

The issue I have is that when I export the.rtf to xml I loose all the adorners and clicking on the image doesn't allow me to resize it.我遇到的问题是,当我将 .rtf 导出到 xml 时,我丢失了所有的装饰物并且单击图像不允许我调整它的大小。

Not working adorner post export导出后装饰器不工作

The export code that appears to break the adorners is:似乎破坏装饰器的导出代码是:

public static readonly DependencyProperty DocumentXamlProperty =
        DependencyProperty.RegisterAttached(
            "DocumentXaml",
            typeof(string),
            typeof(RichTextBoxHelper),
            new FrameworkPropertyMetadata
            {
                BindsTwoWayByDefault = true,
                PropertyChangedCallback = (obj, e) =>
                {
                    var richTextBox = (RichTextBox)obj;

                    // Parse the XAML to a document (or use XamlReader.Parse())
                    var xaml = GetDocumentXaml(richTextBox);
                    var doc = new FlowDocument();
                    var range = new TextRange(doc.ContentStart, doc.ContentEnd);

                    range.Load(new MemoryStream(Encoding.UTF8.GetBytes(xaml)),
                          DataFormats.Rtf);

                    // Set the document (*this breaks the adorners*)
                    richTextBox.Document = doc;

                    // When the document changes update the source
                    range.Changed += (obj2, e2) =>
                    {
                        if (richTextBox.Document == doc)
                        {
                            MemoryStream buffer = new MemoryStream();
                            range.Save(buffer, DataFormats.Xaml);
                            SetDocumentXaml(richTextBox,
                                Encoding.UTF8.GetString(buffer.ToArray()));
                        }
                    };
                }
            });

I wanted to know how I can re-set the adorners on existing images, when I click on them, or not loose the adorners during the conversion to xml when the above code is called.我想知道如何在单击现有图像时重新设置装饰器,或者在调用上述代码时如何在转换为 xml 的过程中不松动装饰器。

Any help appreciated.任何帮助表示赞赏。

Regards, Tithras问候,提特拉斯

Issue resolved, it was during the conversion of the text from rtf to xaml and then back again during the binding, this basically killed the adorners.问题已解决,是在将文本从 rtf 转换为 xaml 的过程中,然后在绑定过程中再次返回,这基本上杀死了装饰器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM