为什么某些控件需要依赖项属性进行绑定,而另一些控件则不需要?

问题描述 投票:0回答:1
我有1个自定义控件,需要将数据绑定到2个不同的UserControls:

<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:

c# wpf xaml data-binding richtextbox
1个回答
0
投票
绑定目标[[必须为DependencyProperty。由于您在RichTextBox的属性上设置了绑定,因此此控件是绑定目标,而Binding.Path指定的属性是源。源可以是通用的CLI属性,也可以是任何.NET对象和XML。
© www.soinside.com 2019 - 2024. All rights reserved.