Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Would a Dependency-Property Implementation Crash My Application When I Provide a Default Value?

Why would a dependency-property implementation crash my application when I provide a default value?

This segment of code is in the class declaration for my UserControl object. Everything works fine - it compiles and runs perfectly.

public static System.Windows.DependencyProperty DepProp
    = System.Windows.DependencyProperty.Register(   "Rect",
                                                    typeof(System.Windows.Shapes.Rectangle),
                                                    typeof(FooControl));
public System.Windows.Shapes.Rectangle Rect
{
    get
    { return ((System.Windows.Shapes.Rectangle)(GetValue(DepProp))); }
    set
    { SetValue(DepProp, value); }
}

However, when I add the default value to the dependency property:
The code compiles, but crashes with a fatal exception when it tries to instantiate the UserControl.

For reference, my code now looks like this - with the PropertyMetaData line added:

public static System.Windows.DependencyProperty DepProp
    = System.Windows.DependencyProperty.Register(   "Rect",
                                                    typeof(System.Windows.Shapes.Rectangle),
                                                    typeof(FooControl),
                                                    new System.Windows.PropertyMetadata(new System.Windows.Shapes.Rectangle()));
public System.Windows.Shapes.Rectangle Rect
{
    get
    { return ((System.Windows.Shapes.Rectangle)(GetValue(DepProp))); }
    set
    { SetValue(DepProp, value); }
}  

Removing the PropertyMetadata from the call to Register() causes the program to function perfectly, without any crashes or any other problems. But I need the default value for later code. How can I get it to accept the default value without crashing?

When it crashes, the following exceptions are shown in the output window:

A first chance exception of type 'System.ArgumentException' occurred in WindowsBase.dll  
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll  
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll  

I need to get this working ASAP, so any advice would be awesome!

like image 513
Giffyguy Avatar asked Jul 22 '09 22:07

Giffyguy


1 Answers

Short answer:

Dependency property default values need to be thread safe (e.g. inherit from System.Windows.Freezable) but System.Windows.Forms.Rectangle isn't.

Long answer:

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/2cb12481-ef86-40b7-8333-443598d89933/

Hint:

If you are using Visual Studio it really helps to let the IDE break on every exception being thrown. Just go to "Debug" -> "Exceptions" and check "Common Language Runtime Exceptions" "Thrown".

Then you'll be prompted and get the exception message which in your case looks like this: "Additional information: Default value for the 'Rect' property cannot be bound to a specific thread."

like image 99
Christian Birkl Avatar answered Nov 15 '22 23:11

Christian Birkl