Sha*_*boo 8 dependency-properties custom-controls wpf-4.5
我创建了一个名为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();
}
[...]
}
Run Code Online (Sandbox Code Playgroud)
(其他DependencyProperties以相同的方式设置并正常工作.)
xaml:
<Address:AddressForm Address="{Binding SelectedMFG.dms_Address, Mode=TwoWay}" ... />
Run Code Online (Sandbox Code Playgroud)
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 { ... }
Run Code Online (Sandbox Code Playgroud)
我的尝试:
我试过以不同的方式访问dms_Address属性.我可以在文本框中显示地址的值,这告诉我datacontext没有问题.
<TextBox Text="{Binding SelectedMFG.dms_Address.StreetName, Mode=TwoWay}" /> <!-- this value displays -->
Run Code Online (Sandbox Code Playgroud)
我也尝试更改依赖属性的注册元数据.我不确定哪个是正确的: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
Run Code Online (Sandbox Code Playgroud)
.
我相信我已经做了一切正确的事情,这应该是有效的.
有什么事情是奇怪的或不正确的吗?
我找到了解决方案。
我将表单上的地址依赖属性从IAddress更改为Object。现在房产正在准备中。看起来,即使我返回IAddress对象,表单实际接收的对象也是dms_Address 的EntityWrapper。
这个实体包装器也会转换为IAddress,所以我不确定它为什么会这样。我猜这只是另一个实体的陷阱。
| 归档时间: |
|
| 查看次数: |
353 次 |
| 最近记录: |