【问题标题】:Why do some controls need Dependency properties for binding while others do not? How to bind RichTextBox.Document? [duplicate]为什么有些控件需要依赖属性进行绑定,而有些则不需要?如何绑定 RichTextBox.Document? [复制]
【发布时间】:2020-05-03 18:04:26
【问题描述】:

我有 1 个自定义控件需要将数据绑定到 2 个不同的用户控件:

        <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 而第一个绑定没有它就像一个魅力。

我有点想在不了解背后原因的情况下解决这个问题,这对我来说是迈向无法理解的黑色巫术的一步 T.T

欢迎任何解释:)

【问题讨论】:

  • A binding can only be defined on a DependencyProperty of a DependencyObject 因为Document 不是DependencyProperty 这就是为什么...Text 是一个dep obj。对于TextBox 以及它为什么起作用。
  • 哦,所以它可能也取决于您绑定的属性...不知道:/我虽然可以绑定任何我可以在后面的代码中修改的属性而没有任何麻烦跨度>
  • 不,你只能绑定到DependencyProperty ...
  • 为了增加精度,您只能绑定一个依赖属性,但您可以绑定任何属性。前者是Binding的目标,后者是源。
  • 您也可以在RichTextBox 中添加FlowDocument,然后您可以绑定到Run.Text。例如&lt;FlowDocument&gt; &lt;Paragraph&gt; &lt;Run Text="{Binding something}"/&gt; &lt;/Paragraph&gt; &lt;/FlowDocument&gt;

标签: c# wpf xaml data-binding richtextbox


【解决方案1】:

绑定的目标必须是DependencyProperty。由于您在RichTextBox 的属性上设置了绑定,因此此控件是绑定目标,而Binding.Path 指定的属性是数据源。源可以是通用 CLI 属性或任何 .NET 对象和 XML 数据。


Read Microsoft Docs: Basic data binding concepts (image source)

FlowDocument 的基本思想是它是可编辑的并且支持富文本格式。由于文档是可编辑的,FlowDocument 的实例预计不会改变。而是实例本身,即用户(或应用程序)修改文档内容。因此设计者选择将其作为 CLI 属性来促进对文档内容的直接编辑,因为加载新的 FlowDocument 实例会严重影响性能(我有根据的猜测)。

如果您仍然需要绑定到文档源,则需要实现附加行为:

RichTextBox.cs

public class RichTextBox : DependencyObject
{
  #region DocumentSource attached property

  public static readonly DependencyProperty DocumentSourceProperty = DependencyProperty.RegisterAttached(
    "DocumentSource", 
    typeof(FlowDocument), 
    typeof(RichTextBox), 
    new PropertyMetadata(default(FlowDocument), RichTextBox.OnDocumentSourceChanged));

  public static void SetDocumentSource(DependencyObject attachingElement, FlowDocument value) => attachingElement.SetValue(RichTextBox.DocumentSourceProperty, value);

  public static FlowDocument GetDocumentSource(DependencyObject attachingElement) => (FlowDocument) attachingElement.GetValue(RichTextBox.DocumentSourceProperty);

  #endregion

  private static void OnDocumentSourceChanged(DependencyObject attachingElement, DependencyPropertyChangedEventArgs e)
  {
    if (!(attachingElement is System.Windows.Controls.RichTextBox richTextBox))
    {
      return;
    }

    richTextBox.Document = e.NewValue as FlowDocument;
  }
}

用法

MainWindow.xaml

<Window>
  <Window.DataContext>
    <ViewModel />
  </Window.DataContext>

  <RichTextBox RichTextBox.DocumentSource="{Binding UserLogs}" />
</Window>

ViewModel.cs

class ViewModel : INotifyPropertyChanged
{
  private FlowDocument userlogs;
  public FlowDocument UserLogs 
  {
    get => return this.userlogs;
    set 
    {
      this.userlogs = value;
      NotifyPropertyChanged();
    }
}

【讨论】:

    猜你喜欢
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-19
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多