<TextBlock
Text="{Binding Title}">
</TextBlock>
<RichTextBox
x:Name="RichTextBoxControl"
Focusable="False"
Document="{Binding UserLogs}">
</RichTextBox>
他们试图绑定到的属性在同一个DataContext中,并且这样定义:
public string Title {
get => return _title;
set {
_title = value;
NotifyPropertyChanged();
}
}
private string _title;
public FlowDocument UserLogs {
get => return _userlogs;
set {
_userlogs = value;
NotifyPropertyChanged();
}
}
private FlowDocument _userlogs;
我在绑定TextBlock时遇到0麻烦(无论是在编译时还是在运行时)。虽然对于RichTextBox的绑定,我在Visual Studio的分析器中出现错误(并且在运行时在UserControl的InitializeComponent()
中):“Impossible to define 'Binding' on the 'Document' property of type 'RichTextBox'. A binding can only be defined on a DependencyProperty of a DependencyObject
”
因此,我非常清楚如何使用DependencyProperty技巧解决该错误,但是我真的不明白这两个绑定之间的区别,为什么在第一个绑定工作时我为什么必须对第二个绑定使用DependencyProperty呢?就像没有它的魅力。
我有点想在不理解背后原因的情况下解决此问题,这对我来说是迈向难以理解的黑人巫术的一步
欢迎任何解释:)
我有1个自定义控件,需要将数据绑定到2个不同的UserControls:
DependencyProperty
。由于您在RichTextBox
的属性上设置了绑定,因此此控件是绑定目标,而Binding.Path
指定的属性是源。源可以是通用的CLI属性,也可以是任何.NET对象和XML。