views:

29

answers:

2

I have a lookless control I've built that has a default style defined in generic.xaml in my Themes directory. I also have the following in the constructor.

 static MyControl()
 {
     DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl), new FrameworkPropertyMetadata(typeof(MyControl)));
 }

 public MyControl()
 {
     //DoSomeWork
 }

Is there something else I'm suppose to set in WPF land? In Silverlight all I have to do is:

DefaultStyleKey = typeof(MyControl);

NOTE: It does render in Expression Blend though.

+1  A: 

Is that constructor static? if not, it should be. The OverrideMetadata call should live in a static constructor to work properly. change or add your constructor like this:

static MyControl()
 {
     DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl), new FrameworkPropertyMetadata(typeof(MyControl)));
 }
Muad'Dib
This is still not working in VS. Still works in Expression Blend.
cjibo
Also note you cannot use access modifiers on static constructors.
cjibo
I should probably note that sometimes the designer in VS is stupid. the 2k10 designer is better then the 2k8, tho.
Muad'Dib
I am running this in 2k10 not 2k8.
cjibo
A: 

Is your VS designer crashes or it shows nothing but a plain border like appearance.

I would suggest. You have a separate style for the control, relatively light weight. Apply that style from constructor of the control by checking whether IsInDesignTime.

If, your designer crashes or displays some error. Then, you should try design time debugging.

Also, in few cases. If, the application type is ".NetFramework 4 Client Profile" [Which is the default, wpf application type in VS2010] you might get some wired things like this.

HTH

Avatar