Detecting Design Mode
A while back, Brian posted an article about how we proposed to implement the equivalent to the Windows Forms Control.DesignMode property in Cider.
In his follow up article, he talks about how we dropped the original proposal and worked with the WPF team to get a the new DesignerProperties class into WPF's PresentationFramework assembly. This change was driven by the feedback he got on his blog -- your feedback makes a difference.
So instead of looking up a DependencyProperty through the AppDomain data context by name, you can check the DependencyProperty defined in the DesignerProperties class. A far better solution that both Cider and Sparkle will support.
When the designer starts up, it will change the default value of the IsInDesignMode attached property from false to true by overriding its metadata. For parts of the designer like the Property Browser or adorner layer that will host controls in a runtime context (as part of the design time UI), the IsInDesignMode property will be set to false on those visual trees.
What that means is that whenever a control is created, it will be able to check whether or not it is in design mode and get back a value of true if that control was created by the designer. This will, for the most part, be the correct and final value, unless that control is hosted in the Property Browser or Adorner context where the IsInDesignMode property will switch from true to false for that control when it is parented to its final visual tree.
Getting the IsInDesignMode Property
Here is a simple example of making a runtime versus design time decision using the DesignerProperties class:
public class CustomButton : Button
{
public CustomButton()
{
if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
{
Content = "In Design Mode";
}
else
{
Content = "Runtime";
}
}
}
Note how this code can be run from the constructor.
Setting the IsInDesignMode Property
Isn't setting the IsInDesignMode property the responsiblity of the designer? In most cases, yes... but not all cases.
Consider the situation where a control has design time adorners that popup a dialog when clicked. Since the dialog is not in the visual tree of the adorner layer, it will get the default value of the IsInDesignMode property which is set to true when the designer starts up.
In this case, the control developer will want to set the IsInDesignMode property to false for that dialog so that the controls on that dialog operate in a runtime context.
The DesignerProperties class has a SetIsInDesignMode property to accommodate this kind of scenario:
System.ComponentModel.DesignerProperties.SetIsInDesignMode(element, newValue);
Tracking Changes to the IsInDesignMode Property
It is important to track changes to the IsInDesignMode property as its initial value may not be its final value -- the Property Browser Editor or Adorner Layer example mentioned above.
For example, the value of the IsInDesignMode property when your custom control is instantiated by Cider will almost always be true.
If that control is being used within the Cider Property Browser in an Extended Editor you've written, it will change to false when that control is added into the Property Browsers visual tree where the IsInDesignMode property is false.
To track state changes of the IsInDesignMode property, override OnPropertyChanged.
Comments
Anonymous
October 07, 2006
Good design! [url=http://kahgkbcu.com/wbol/qgpn.html]My homepage[/url] | [url=http://dvpmshdf.com/snzd/pkwa.html]Cool site[/url]Anonymous
October 07, 2006
Thank you! <a href="http://kahgkbcu.com/wbol/qgpn.html">My homepage</a> | <a href="http://mnvhtnfv.com/pswx/qpmt.html">Please visit</a>Anonymous
October 07, 2006
Good design! http://kahgkbcu.com/wbol/qgpn.html | http://ifvgwavm.com/vquf/luza.htmlAnonymous
October 21, 2006
Is there any way to make Cider not open XAML files in mixed Design/Source mode? For the type of editing I do, design mode has never mattered, and I really just want to edit the source, but it takes about 3 minutes to work through the xaml when opening vs. All I want is the xml source, but I also want the xsl based autocompletion, so I can't use the source editor.Anonymous
November 14, 2006
Well done! [url=http://gwlqfzbg.com/vdao/pmvc.html]My homepage[/url] | [url=http://fnnjvapr.com/irgq/hqpn.html]Cool site[/url]Anonymous
November 14, 2006
Thank you! <a href="http://gwlqfzbg.com/vdao/pmvc.html">My homepage</a> | <a href="http://ohwkezyt.com/glxo/rigb.html">Please visit</a>Anonymous
November 14, 2006
Great work! http://gwlqfzbg.com/vdao/pmvc.html | http://gclvmuuf.com/qpqo/tzoe.htmlAnonymous
November 14, 2006
Well done! [url=http://hvzqlxwd.com/krhm/mxwz.html]My homepage[/url] | [url=http://scnqlnuk.com/zygl/kndr.html]Cool site[/url]Anonymous
November 14, 2006
Great work! <a href="http://hvzqlxwd.com/krhm/mxwz.html">My homepage</a> | <a href="http://bqrcxtzt.com/qmwf/iiqr.html">Please visit</a>Anonymous
November 14, 2006
Thank you! http://hvzqlxwd.com/krhm/mxwz.html | http://pqurazsc.com/uktf/svhu.htmlAnonymous
September 24, 2007
There are a ton of WPF samples on MSDN which are really useful if you are developing in or learning WPF