在读了肯特·布加特的答案之后
this
问题我认为将SetValue更改为SetCurrentValue的正确位置不在CLR属性中,而在MarkDownEditor的构造函数中。
public MarkdownEditor()
{
InitializeComponent();
this.SetCurrentValue(OptionsProperty, new MarkdownEditorOptions());
DataContext = this;
}
事实上,如果没有this.SetCurrentValue,这也同样有效,因为选项将通过绑定设置。
要验证您的绑定实际上已被SetValue覆盖,您可以在某些情况下为TabUsage添加此代码(例如,为FontSize文本框添加PreviewMouseRightButtonDown),绑定将重新开始工作。
private void TextBox_PreviewMouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
MarkdownEditor.MarkdownEditor editor = VisualTreeHelpers.GetVisualChild<MarkdownEditor.MarkdownEditor>(this);
Binding binding = new Binding();
binding.Path = new PropertyPath("Options");
binding.Source = this;
binding.Mode = BindingMode.TwoWay;
editor.SetBinding(MarkdownEditor.MarkdownEditor.OptionsProperty, binding);
}