WPF自定义控件:从未设置DependencyProperty(仅在多个属性中的一个上) [英] WPF Custom Control: DependencyProperty never Set (on only 1 of many properties)

查看:168
本文介绍了WPF自定义控件:从未设置DependencyProperty(仅在多个属性中的一个上)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个自定义控件,称为 AddressForm ,该控件继承自Control。该控件用于显示IAddress对象的字段。

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

最初我是在Silverlight中进行此控制的,现在我正试图使其在WPF .net 4.5中正常工作

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

该控件定义了9个不同的依赖项属性,除一个属性外,其他所有属性均正常工作。自然,不起作用的是地址对象本身!

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!

Control的Address属性从不接收值。指向地址的Getter,该属性被调用,该地址对象不为null,但控件没有收到它。

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.

控制:

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的设置方式相同且可以正常运行。)

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

xaml:

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

SelectedMFG的类型为scm_MFG

the type of SelectedMFG is 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属性。我可以在文本框中显示地址的值,这告诉我数据上下文没有问题。

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 -->

我也尝试过更改依赖项属性的注册元数据。
我不确定要使用哪一种正确的方法: PropertyMetadata,UIPropertyMetadata FrameworkPropertyMetadata

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找到了解决方案。

I found a solution.

我将表单上的地址相关性属性从 IAddress 更改为 Object 。现在该属性已设置。看来,即使我返回的是 IAddress 对象,表单实际接收的对象还是dms_Address的 EntityWrapper

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.

该实体包装器也将强制转换为 IAddress ,因此我不确定为什么会这样。我猜只是另一个实体陷阱。

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.

这篇关于WPF自定义控件:从未设置DependencyProperty(仅在多个属性中的一个上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆