WPF 依赖属性 - 数据绑定不起作用

发布于 2024-11-28 09:14:09 字数 1264 浏览 1 评论 0原文

正如标题所示,我在使用具有 DependencyProperty 的数据绑定时遇到问题。我有一个名为 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;
    }
}

我在构造函数中读取 Text 属性,因此当字符串绑定到该属性时,它应该显示为文本。但即使我将一些数据绑定到 xaml 中的 Text 属性,我什至没有看到设置 Text 属性时应显示的“Setter...”消息。

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

如果我将 HTMLBox 更改为 TextBox,文本将正确显示,因此错误可能出在我的 HTMLBox 类中的某个位置。我做错了什么?

As the title already says I am having trouble using databinding with a DependencyProperty. I have a class called 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. 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.

    <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. What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

靑春怀旧 2024-12-05 09:14:09

这里存在一些问题:

  1. 您不应该将逻辑放置在包装依赖属性的 CLR 属性的 set / get 中。此属性只是为了提供更方便的机制来获取/设置依赖属性。无法保证 XAML 解析器将调用此 setter。如果您需要在依赖属性更改时调用任何逻辑,请在通过 DependencyProperty.Register 注册依赖属性时通过更改事件处理程序执行此操作。
  2. 您在构造函数中构建控件的 UI,这里有计时问题!要构造类的实例,首先调用构造函数,然后设置各种属性。 Text 将始终是构造函数中的默认值。再次,与 (1) 类似的解决方案,当您的 Text 属性更改时,重建/更新您的 UI。

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. 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. 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.
  2. You build your control's UI in the constructor, you have timing issue here! 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. Again, a similar solution to (1), when your Text property changes, rebuild / update your UI.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文