WPF Dependency Property Data Binding Problem

data-bindingwpfxaml

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));

Best Answer

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.