我创建了一个名为 AddressForm 的自定义控件,它继承自Control。该控件用于显示IAddress对象的字段。
最初我在Silverlight中制作了这个控件,现在我试图让它在WPF .net 4.5中运行
该控件定义了9个不同的依赖项属性,除了一个外,其他所有属性都正常工作。当然,不起作用的是Address对象本身!
Control的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属性。我可以在文本框中显示地址的值,这告诉我datacontext没有问题。
<TextBox Text="{Binding SelectedMFG.dms_Address.StreetName, Mode=TwoWay}" /> <!-- this value displays -->
我还尝试更改依赖项属性的注册元数据。 我不确定哪个是正确的: PropertyMetadata,UIPropertyMetadata 或 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
我相信我已经正确地完成了所有事情并且应该正在运作。
有什么事情是奇怪的或不正确的吗?
答案 0 :(得分:2)
我找到了解决方案。
我将表单上的地址依赖项属性从 IAddress 更改为对象。现在该物业正在设定。似乎即使我返回 IAddress 对象,表单实际接收的对象是dms_Address的 EntityWrapper 。
这个实体包装器也会转换为 IAddress ,所以我不确定它为什么会这样。我想是另一个实体问题。