Document 已属于另一 RichTextBox 或者 Document RichTextBox already belongs to another RichTextBox

最近在使用RichTextBox时,因为是MVVM模式,要绑定它的Document,但Document不是依赖属性,就重新写了个类BindableRichTextBox,继承自RichTextBox。起初一切顺利,但后面发现在设计视图时,总是报下面这个错误:

ArgumentException: Document 已属于另一 RichTextBox;

如果是英文版vs的话,应该报的是

Document already  belongs to another RichTextBox;

查阅了很多官方文档,最后发现是因为不能共享实例。

故在类声明出,增加标记:[PartCreationPolicy(CreationPolicy.NonShared)]

命名空间:   System.ComponentModel.Composition
程序集:  System.ComponentModel.Composition(位于 System.ComponentModel.Composition.dll))

原文地址:https://www.cnblogs.com/zhangyongheng/p/6474149.html