WPF依赖属性数据绑定问题(WPF Dependency Property Data Binding Problem)

我在WPF中创建了一个UserControl。 此用户控件具有多个文本框,这些文本框绑定到数据库对象上的属性,该对象由UserControl上的proptery引用。 xaml看起来像:

<TextBox Name="_txtFirstName" Text="{Binding Path=Contact.FirstName, UpdateSourceTrigger=PropertyChanged}"/>

这工作正常,直到我将Contact属性变为依赖属性,以便我可以将它绑定到ListBox中的选定项。 一旦我这样做了TextBoxes的绑定停止工作。 为什么是这样?

DependencyProperty代码是:

public static readonly DependencyProperty ContactProperty = DependencyProperty.Register( "Contact", typeof(Contacts), typeof(ContactView));

I created a UserControl in WPF. This user control has several text boxes that are bound to properties on a database object which is referenced by proptery on the UserControl. The xaml looks like:

<TextBox Name="_txtFirstName" Text="{Binding Path=Contact.FirstName, UpdateSourceTrigger=PropertyChanged}"/>

This worked properly until I made the Contact property into a dependency property so that I could bind it to the selected item in a ListBox. Once I did this the binding of the TextBoxes stopped working. Why is this?

The DependencyProperty code was:

public static readonly DependencyProperty ContactProperty = DependencyProperty.Register( "Contact", typeof(Contacts), typeof(ContactView));

最满意答案

我解决了这个问题。 我忘了更改此代码:

    public Contacts Contact
    {
        get { return _contact; }
        set { _contact = value; }
    }
 

对此:

    public Contacts Contact
    {
        get { return (Contacts)GetValue(ContactProperty); }
        set { SetValue(ContactProperty, value); }
    }
 

现在它正常工作。

I figured out the problem. I forgot to change this code:

    public Contacts Contact
    {
        get { return _contact; }
        set { _contact = value; }
    }
 

To this:

    public Contacts Contact
    {
        get { return (Contacts)GetValue(ContactProperty); }
        set { SetValue(ContactProperty, value); }
    }
 

Now it works properly.

WPF依赖属性数据绑定问题(WPF Dependency Property Data Binding Problem)

我在WPF中创建了一个UserControl。 此用户控件具有多个文本框,这些文本框绑定到数据库对象上的属性,该对象由UserControl上的proptery引用。 xaml看起来像:

<TextBox Name="_txtFirstName" Text="{Binding Path=Contact.FirstName, UpdateSourceTrigger=PropertyChanged}"/>

这工作正常,直到我将Contact属性变为依赖属性,以便我可以将它绑定到ListBox中的选定项。 一旦我这样做了TextBoxes的绑定停止工作。 为什么是这样?

DependencyProperty代码是:

public static readonly DependencyProperty ContactProperty = DependencyProperty.Register( "Contact", typeof(Contacts), typeof(ContactView));

I created a UserControl in WPF. This user control has several text boxes that are bound to properties on a database object which is referenced by proptery on the UserControl. The xaml looks like:

<TextBox Name="_txtFirstName" Text="{Binding Path=Contact.FirstName, UpdateSourceTrigger=PropertyChanged}"/>

This worked properly until I made the Contact property into a dependency property so that I could bind it to the selected item in a ListBox. Once I did this the binding of the TextBoxes stopped working. Why is this?

The DependencyProperty code was:

public static readonly DependencyProperty ContactProperty = DependencyProperty.Register( "Contact", typeof(Contacts), typeof(ContactView));

最满意答案

我解决了这个问题。 我忘了更改此代码:

    public Contacts Contact
    {
        get { return _contact; }
        set { _contact = value; }
    }
 

对此:

    public Contacts Contact
    {
        get { return (Contacts)GetValue(ContactProperty); }
        set { SetValue(ContactProperty, value); }
    }
 

现在它正常工作。

I figured out the problem. I forgot to change this code:

    public Contacts Contact
    {
        get { return _contact; }
        set { _contact = value; }
    }
 

To this:

    public Contacts Contact
    {
        get { return (Contacts)GetValue(ContactProperty); }
        set { SetValue(ContactProperty, value); }
    }
 

Now it works properly.