In WPF XAML there is the convenient DesignHeight
and DesignWidth
, for instance in code as
<UserControl ... d:DesignHeight="500" d:DesignWidth="500" ... />
which is great because I can build the layout with a representative, but not locked-in, control size.
However, I'm often building dark UIs, where labels and so forth need to be white, but my controls still need a transparent background color. This creates a design-time inconvenience because white seems to be the default background color for transparent controls in the designer, leading to unreadable white-on-white labels.
Is there a way or strategy for setting the design-time background color, with similar convenience as DesignHeight/DesignWidth?
My answer was found here: Black Background for XAML Editor. There are a number of choices including checking
System.ComponentModel.DesignerProperties.GetIsInDesignMode(this)
at runtime.There's an undocumented property
d:DesignStyle
of typeStyle
that you can set on a user control. This style is only applied in the designer and is not used at runtime.You use it like this:
Or like this:
Note however that any value set on the
Style
property (the one used at runtime) will also override theDesignStyle
in the designer.This is the complete solution for DesignBackground:
Usage:
I found that you can do one for yourself. Custom design-time attributes in Silverlight and WPF designer is a tutorial how to do it for both Silverlight and WPF.
The
d:DesignerProperties.DesignStyle
technique shown on this page works great for applying a WPF design-time-only style to a single control, but it doesn't appear to work for aStyle
in aResourceDictionary
that would apply to all of the appropriately-typed controls or elements under the scope of the dictionary. Below is simple solution I found for deploying a designer-only style into aResourceDictionary
.Consider for example a
Window
containing aTreeView
, where we want theTreeViewItem
nodes to show as fully expanded—but only at design time. First, put the desired style in the XAML dictionary in the normal way.Here, the
Style
is put in theResourceDictionary
of theWindow
but of course you could use any other subsuming dictionary instead. Next, in the C# code, remove the style from theResourceDictionary
when design mode is not detected. Do this is in theOnInitialized
override:Design Mode: Runtime Mode: