【发布时间】:2010-01-20 14:28:57
【问题描述】:
我想设置我的附加属性的默认值,但是当我这样做时:
“System.ArgumentException”类型的第一次机会异常发生在 WindowsBase.dll 中
在 Oef_AttDepProp.exe 中发生了“System.TypeInitializationException”类型的第一次机会异常
没有默认值,一切正常。 这是我使用的一些示例代码:
public static readonly DependencyProperty IsEigenaarProperty = DependencyProperty.RegisterAttached(
"Eigenaar", typeof(clsPersoon), typeof(UIElement),
new UIPropertyMetadata(new clsPersoon("test", "test"), PropertyChanged));
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Browsable(true)]
public clsPersoon Eigenaar
{
get
{
return _persoon;
}
set
{
_persoon = value;
}
}
public static void SetEigenaar(UIElement element, clsPersoon value)
{
element.SetValue(IsEigenaarProperty, value);
}
public static clsPersoon GetEigenaar(UIElement element)
{
return (clsPersoon)element.GetValue(IsEigenaarProperty);
}
private static void PropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
if (obj is Window1)
((Window1)obj).Title = GetEigenaar(((Window1)obj)).ToString();
}
“new clsPersoon("test", "test")" 似乎是导致问题的原因,但这只是一个非常简单的带有 2-string-constructor 的类。
编辑:当尝试通过单击事件而不是 window_load 设置属性时,我得到一个 innerException:“'Eigenaar' 属性的默认值不能绑定到特定线程。”
【问题讨论】:
标签: c# wpf attached-properties argumentexception