Two Help Classes for WPF Regarding Design
Check if the code is runing from the Designer and set values only to be seen in the Designer.
Introduction
This article shows a combination of two helper classes I wrote for WPF.
Background
For various reasons, I needed to set values to an element only to be shown in the Designer. This is done by the first class. The second class is used by the first to check whether code is running under the Designer.
Set a value to a control only in Design mode
Can you set a value to the DependencyProperty of a DependencyObject in Design mode? Using a custom MarkupExtension
, you can. Here is the implementation:
[ContentProperty("Value")]
public class DesignTimeDummy : MarkupExtension
{
public DesignTimeDummy()
{
}
public object Value { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (Helpers.Designer.InDesignMode)
{
return Value;
}
return DependencyProperty.UnsetValue;
}
}
Here are two examples for the XAML code:
<Button Style="{DynamicResource styleName}"
Content="{CoreME:DesignTimeDummy Value=DesignContentValue}"/>
or
<Button Style="{DynamicResource styleName}" >
<Button.Content>
<CoreME:DesignTimeDummy>
Dummy </CoreME:DesignTimeDummy>
</Button.Content>
</Button>
where CoreME
is an xmlns
defined namespace shortcut.
Check to see if it is in Design mode
The above code uses this class:
public static class Designer
{
private static DependencyObject dummy = new DependencyObject();
public static bool InDesignMode
{
get { return DesignerProperties.GetIsInDesignMode(dummy); }
}
}
Hope you find them useful.