I created a simple WPF user element that inherits from Window, so that later I can override its chrome and reuse it in my projects. The control is contained in its own project along with the generic.xaml file (in the Themes directory ). The style of the control simply sets a red background. In the second project (same solution), I then use the control as the base class of the new WPF window. When I compile the project now and run it, it shows my (custom) window with the desired red background.
Now my question is: how do I get the WPF constructor to show a common style (in this case, a red background) during development?
When I do the same, for example with a button, then the general style is applied automatically during development. But any default style of the control that inherits from the window will be displayed only at runtime. What am I missing?
control code:
public class CustomControl1 : Window
{
static CustomControl1()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
}
}
Generic.xaml:
<Style TargetType="{x:Type local:CustomControl1}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl1}">
<Border Background="Red"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
XAML of the test window in the second project, where I use the control:
<snh:CustomControl1 x:Class="WpfDockWindow.TestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:snh="clr-namespace:SnH.WpfControls;assembly=SnH.WpfControls"
Title="TestWindow" Height="300" Width="300">
<Grid>
</Grid>
And for the records I use VS2013, and the projects are compiled with the 4.5.1 framework.
thanks Ben
source
share