Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 704
Shaboboo Avatar asked Jul 17 '15 18:07

Shaboboo


1 Answers

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.

like image 172
Shaboboo Avatar answered Dec 29 '22 10:12

Shaboboo