WPF Custom Control: DependencyProperty never Set (on only 1 of many properties)
Asked Answered
B

1

8

I have made a custom control called AddressForm, which inherits from Control. The control is used to display the fields for an IAddress object.

Originally I made this control in Silverlight, now I am trying to get it working in WPF .net 4.5

The control defines 9 different dependency properties, and all but one is working correctly. Naturally, the one that is not working is the Address object itself!

The Control's Address property never receives a value. I put a break-point in the address's Getter, the property is getting called, the address object is not null, but the control doesn't receive it.

There are no exceptions, nor error messages in the output screen.

Control :

public class AddressForm : Control, INotifyPropertyChanged
{
    [...]

    public static readonly DependencyProperty AddressProperty = DependencyProperty.Register("Address", typeof(IAddress), typeof(AddressForm), new PropertyMetadata( AddressChanged));
    public IAddress Address 
    {
        get { return (IAddress)GetValue(AddressProperty); }
        set { SetValue(AddressProperty, value); } 
    }

    private static void AddressChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        //break-point here never gets hit
        AddressForm form = d as AddressForm;
        if (form != null)
            form.OnAddressSet();
    }

    private void OnAddressSet()
    {
        //break-point here never gets hit
        if (StateProvince != null && Address != null)
        SelectedStateProvince = StateProvince.Where(A => A.StateProvince == Address.StateProvince).FirstOrDefault();
    }

    [...]
}

(Other DependencyProperties are set in the same way and work correctly.)

xaml :

<Address:AddressForm Address="{Binding SelectedMFG.dms_Address, Mode=TwoWay}" ... />

the type of SelectedMFG is scm_MFG

Data objects:

public partial class scm_MFG 
{
    [...]
    public virtual dms_Address dms_Address { get; set; } //break-point here never enables? Generated code from Entity TT

    //Another attempt, trying to determine if the IAddress cast was the cause of the issue
    //Address="{Binding SelectedMFG.OtherAddress}" 
    public IAddress OtherAddress 
    {
        get { 
            return dms_Address as IAddress; //break-point here gets hit. dms_Address is not null. Control never receives the value.
        } 
    }
}

public partial class dms_Address : IAddress, INotifyPropertyChanged { ... }

My Attempts:

I have tried accessing the dms_Address property different ways. I can display the values of the Address in a textbox, which tells me there is no problem with the datacontext.

<TextBox  Text="{Binding SelectedMFG.dms_Address.StreetName, Mode=TwoWay}" /> <!-- this value displays -->

I have also tried changing the dependency property's registration metadata. I'm not sure which is the correct one to use: PropertyMetadata, UIPropertyMetadata or FrameworkPropertyMetadata

DependencyProperty.Register("Address", typeof(IAddress), typeof(AddressForm), new PropertyMetadata(AddressChanged));
DependencyProperty.Register("Address", typeof(IAddress), typeof(AddressForm), new PropertyMetadata(null, AddressChanged));
DependencyProperty.Register("Address", typeof(IAddress), typeof(AddressForm), new UIPropertyMetadata(AddressChanged));
DependencyProperty.Register("Address", typeof(IAddress), typeof(AddressForm), new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits, AddressChanged));
// and other FrameworkPropertyMetadataOptions, no difference

.

I believe I have done everything correctly and that this should be working.

Does anything jump out as odd or incorrect?

Buffalo answered 17/7, 2015 at 18:40 Comment(0)
B
2

I found a solution.

I changed the Address Dependency Property on the form from IAddress to Object. Now the property is getting set. It seems that even though I was returning an IAddress object, the object that the form is actually receiving is an EntityWrapper for dms_Address.

This entity wrapper will cast to IAddress too, so I'm not sure why it was behaving like this. Just another Entity gotcha I guess.

Buffalo answered 22/7, 2015 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.