WPF自定义控件:DependencyProperty永远不会设置(只对许多属性中的一个设置)



我做了一个自定义控件叫做AddressForm,它继承自control。该控件用于显示IAddress对象的字段。

最初我在Silverlight中制作了这个控件,现在我试图让它在WPF .net 4.5中工作

控件定义了9个不同的依赖属性,除了一个之外,其他的都正常工作。当然,不能工作的是Address对象本身!

控件的地址属性从不接收值。我在地址的Getter中放置了一个断点,该属性正在被调用,地址对象不是null,但控件没有接收它。

输出屏幕中没有异常,也没有错误消息。

控制:

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();
    }
    [...]
}

(其他DependencyProperties以相同的方式设置并正确工作)

xaml:

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

SelectedMFG的类型是scm_MFG

数据对象:

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 { ... }
我尝试

:

我已经尝试访问dms_Address属性不同的方式。我可以在文本框中显示Address的值,这告诉我数据上下文没有问题。

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

我还尝试更改依赖属性的注册元数据。我不确定哪一个是正确的使用:PropertyMetadata, UIPropertyMetadataFrameworkPropertyMetadata

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

.

相信我已经做了正确的一切,这个应该工作。

是否有奇怪或不正确的东西跳出来?

我找到了一个解决方案。

我改变了地址依赖属性的形式从IAddressObject。现在性质被设定了。似乎即使我返回一个IAddress对象,该表单实际接收的对象是dms_Address的EntityWrapper

这个实体包装器将转换为IAddress,所以我不确定为什么它的行为是这样的。

最新更新