开发者

Attached Property: 'System.TypeInitializationException' when setting default value

I want to set a default value of my Attached Property, but when I do I get:

A first chance exception of type 'System.ArgumentException' occurred in WindowsBase.dll

A first chance exception of type 'System.TypeInitializationException' occurred in Oef_AttDepProp.exe

Without the default value, things work fine. This is some sample code I used:

public static readonly DependencyProperty IsEigenaarProperty = DependencyProperty.RegisterAttached(
"Eigenaar", typeof(clsPersoon), typeof(UIElement), 
new UIPropertyMetadata(new clsPersoon("test", "test"), PropertyChanged));

[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
[Browsable(true)]
publi开发者_运维知识库c 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();
}

It's the "new clsPersoon("test", "test")" which seems to be the cause the problem, but that is only a very simple class with a 2-string-constructor.

Edit: When trying to set the property through a click event, instead of the window_load, I get an innerException of: "Default value for the 'Eigenaar' property cannot be bound to a specific thread."


Typically exceptions of type TypeInitializationException are thrown when an exception occurs in the static constructor. Look there.

Also, from the inner exception:

Default value for the Eigenaar property cannot be bound to a specific thread.

This usually means that your property is not thread-safe (e.g., doesn't inherit from System.Windows.Freezable). Check this thread for gory details and MSDN for details about default values for dependency properties.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜