晚上好!
我有一个继承自WPF扩展工具包的richTextBox的类,我在其中添加了一个ObservableCollection类型的依赖项属性和正确的包装器 . 我想使用此属性来存储InlineUIContainers的文本,我将其添加到FlowDocument并将它们数据绑定到ObservableCollection变量 .
我的问题是这个richTextBox的所有实例都将它们的数据存储在同一个地方(单例) .
我已经尝试使用List,在构造函数中初始化属性(如下面的代码示例中所示) - 也使用SetValue . 我已将FrameworkMetadataProperty默认值设置为null并且仍然没有运气(在这种情况下,它实际上在尝试添加项目时会给我“对象未初始化”错误) .
我已经没想完了 . 我在过去的两个小时里一直在谷歌搜索它,但我仍然无法弄清楚为什么实例没有独立存储值 .
任何帮助是极大的赞赏 .
public class RichTextBoxWithDataBoundableTokens : Xceed.Wpf.Toolkit.RichTextBox
{
public static readonly DependencyProperty TokensContentsProperty = DependencyProperty.Register("TokensContents", typeof(ObservableCollection<object>), typeof(RichTextBox), new FrameworkPropertyMetadata(new ObservableCollection<object>()));
public ObservableCollection<object> TokensContents { get { return (ObservableCollection<object>)GetValue(TokensContentsProperty); } set { SetValue(TokensContentsProperty, (ObservableCollection<object>)value); } }
public RichTextBoxWithDataBoundableTokens()
: base()
{
TokensContents = new ObservableCollection<object>();
}
}
这就是我使用Control的方式:
RichTextBoxWithDataBoundableTokens test = new RichTextBoxWithDataBoundableTokens()
test.TokensContents.Add("some object");
1 回答
您在Register方法中设置typeOf(RichTextBoxWithDataBoundableTokens)而不是typeOf(RichTextBox) . 我希望这将有所帮助 .