简体   繁体   English

WPF依赖项属性-Databinding不起作用

[英]WPF Dependency Property -Databinding doesn't work

As the title already says I am having trouble using databinding with a DependencyProperty. 正如标题所说,我在使用DependencyProperty进行数据绑定时遇到问题。 I have a class called HTMLBox: 我有一个名为HTMLBox的类:

public class HTMLBox : RichTextBox
{
    public static readonly DependencyProperty TextProperty = 
    DependencyProperty.Register("Text", typeof(string), typeof(HTMLBox));

    public string Text
    {
        get
        {
            return GetValue(TextProperty) as string;
        }
        set
        {
            Console.WriteLine("Setter...");
            SetValue(TextProperty, value);
        }
    }

    public HTMLBox()
    {
        // Create a FlowDocument
        FlowDocument mcFlowDoc = new FlowDocument();

        // Create a paragraph with text
        Paragraph para = new Paragraph();
        para.Inlines.Add(new Bold(new Run(Text)));

        // Add the paragraph to blocks of paragraph
        mcFlowDoc.Blocks.Add(para);

        this.Document = mcFlowDoc;
    }
}

I reading the Text-property in the Constructor, so it should be displayed as text when a string is bound to the property. 我在构造函数中读取Text属性,因此当字符串绑定到属性时,它应显示为文本。 But even though I bind some data to the Text property in xaml, I don't even see the "Setter..."-Message which should be shown when the Text-property is set. 但即使我将一些数据绑定到xaml中的T​​ext属性,我甚至看不到“Setter ...” - 应该在设置Text-property时显示的消息。

    <local:HTMLBox Text="{Binding Text}" 
           Width="{Binding Width}"  
           AcceptsReturn="True" 
           Height="{Binding Height}" />

If I change HTMLBox to TextBox the text is displayed properly, so the mistake is probably somwhere in my HTMLBox class. 如果我将HTMLBox更改为TextBox,则文本显示正确,因此错误可能是我的HTMLBox类中的错误。 What am I doing wrong? 我究竟做错了什么?

You have a few issues going on here: 你有几个问题在这里:

  1. You should not place logic in the set / get of your CLR property which wraps your dependency property. 您不应该将逻辑放在包装依赖项属性的CLR属性的set / get中。 This property is only there to provide a more convenient mechanism for getting / setting your dependency property. 此属性仅用于提供更方便的获取/设置依赖项属性的机制。 There is no guarantee that the XAML parser will invoke this setter. 无法保证XAML解析器将调用此setter。 If you need to invoke any logic when your dependency property is changed, do this via a change event handler when you register your dependency property via DependencyProperty.Register . 如果需要在更改依赖项属性时调用任何逻辑,请在通过DependencyProperty.Register注册依赖项属性时通过更改事件处理程序执行此操作。
  2. You build your control's UI in the constructor, you have timing issue here! 你在构造函数中构建控件的UI,这里有时间问题! To construct an instance of your class the constructor is first called, then the various properties are set. 要构造类的实例,首先调用构造函数,然后设置各种属性。 Text will always be the default value in the constructor. Text将始终是构造函数中的默认值。 Again, a similar solution to (1), when your Text property changes, rebuild / update your UI. 同样,对于(1)的类似解决方案,当Text属性更改时,重建/更新UI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM