Summary of WPF designer limitations and rules for WYSIWYG view creation?

474 Views Asked by At

For WPF/Silverlight/XAML4Win8/WP8/whathaveyou, the visuals are created by (I believe) newing up an instance of the base class that your custom view (window/page/usercontrol/whathaveyou) is derived from, and then applying your XAML after the fact.

If I'm not mistaken this means codebehind in the type's constructor is lost. Is there a way to execute design-time object creation logic in the view itself? More importantly is there a good summary online somewhere of how the Cider/Blend designers actually create the WYSIWYG views at design time? I seem to recall some documentation on this somewhere (Expression Studio docs maybe?) But I can't find them for the life of me.

2

There are 2 best solutions below

3
On

http://msdn.microsoft.com/en-us/library/ff602274(v=vs.95).aspx

The above link applies to Silverlight, but pretty sure most if not all applies to WPF as well.

You can instantiate a designer DataContext.

    <Grid x:Name="LayoutRoot" Background="White" 
          d:DataContext="{d:DesignInstance Type=local:Customer}">

One limitation is that it requires you to have a default constructor. Here is an answer I found to get around it. How to use d:DesignInstance with types that don't have default constructor?.

Best would be to subclass the data you are coding against and have it do any necessary initializing. Luckily everything you are defining is purely for the designing and has no effect on what objects you are actually working with at runtime.

Unfortunately I do not have answers for the rest of the questions.

0
On

what kind of stuff do you need to do in the constructor?

Could you do it by adding either dependency or attached properties with DependencyPropertyChanged hooks and setting values in the xaml?

Then you’d still get your code executing, but not the constructor?