5

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.

Firoso
  • 6,647
  • 10
  • 45
  • 91
  • I think, visuals creation at design time and run time differs. As described here - http://msdn.microsoft.com/en-us/library/aa970678%28v=vs.110%29.aspx - build task parses XAML and compiles it with several files as the result. This means, if your assumption is true, that every edit in XAML runs msbuild... This will be significant performance hit. Am I wrong? May be, designers parse XAML and render visuals without any compilation? Also, XAML can contain errors in markup (which will cause compilation errors), but designer still shows the view... – Dennis Jan 10 '14 at 21:08

2 Answers2

1

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.

Community
  • 1
  • 1
TyCobb
  • 8,909
  • 1
  • 33
  • 53
  • The issue with this is you are ASSUMING a data context exists. While this is not a bad assumption, it's not a good one either. Even in MVVM friendly design, you shouldn't assume that MVVM is being utilized or that binding contexts are being leveraged. If you are properly separating design from logic as in the parts and states model, you shouldn't be making any datacontext assumptions at all. My issue therefore is not around the MVVM pattern, but rather simply how WYSIWYG Views are being created by the designer, and how it can be coerced into executing object creation logic for custom ctrls – Firoso Jan 10 '14 at 21:40
  • @Firoso I guess I misunderstood the question since I never use a WYSIWYG designer. If you think this answer doesn't apply at all, I will delete it. – TyCobb Jan 10 '14 at 21:50
  • I like your response, I just feel you're addressing the wrong question. In short what I asked was how the designer instantiates types for design time editing. Not how design time types can be provided as a binding context. One is well documented, one is not ;-) – Firoso Jan 13 '14 at 16:39
0

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?

John Gardner
  • 24,225
  • 5
  • 58
  • 76