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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With