WPF user control: DependencyProperty is never set (only for one of many properties)

I created my own AddressForm control that inherits from Control. The control is used to display fields for the IAddress object.

I originally made this control in Silverlight, now I'm trying to get it to work in WPF.net 4.5

The control defines 9 different dependency properties, and all but one work correctly. Naturally, the one that doesn't work is the Address! Object itself.

The Control Address property never gets a value. I set a breakpoint at the Getter address, the property receives the call, the address object is not null, but the control does not receive it.

There are no exceptions or error messages on 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 properties are set the same and work correctly.)

xaml:

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

type SelectedMFG - 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 tried to access the dms_Address property in many ways. I can display the address values ​​in a text box, which means there is no problem with the datacontext.

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

I also tried changing the registration metadata of dependency properties. I'm not sure which one is suitable: PropertyMetadata, UIPropertyMetadata strong> or FrameworkPropertyMetadata strong>

 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 did everything right and that should work.

Is something jumping like weird or wrong?

+8
custom-controls dependency-properties
source share
1 answer

I have found a solution.

I changed the Address Dependency Property in the form from IAddress to Object . Now the property gets a lot. It seems that although I was returning an IAddress object, the object that the form actually receives is EntityWrapper for dms_Address.

This object wrapper will be added to IAddress , so I'm not sure why it behaved that way. I think one more Essence.

+2
source share

All Articles