Custom WPF window does not apply its common template at design time

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

+4
source share
2 answers

? Window ContentControl, Window .

namespace ProjectBar.Controls
{
    public class CustomControl : ContentControl
    {
        static CustomControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl), new FrameworkPropertyMetadata(typeof(CustomControl)));
        }
    }
}

ProjectBar\Themes\Generic.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:controls="clr-namespace:ProjectBar.Controls">
  <Style TargetType="{x:Type controls:CustomControl}">
    <Setter Property="Background" Value="Red"/>
    <Setter Property="Template">
      <Setter.Value>
        <ControlTemplate TargetType="{x:Type controls:CustomControl}">
          <Grid Margin="5" Background="{TemplateBinding Background}">
            <Grid.RowDefinitions>
              <RowDefinition Height="*"/>
              <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>

            <!--Define the ContentPresenter. This is the place where the Content goes-->
            <ContentPresenter Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Center"/>

            <!--Place the OK and Cancel buttons-->
            <StackPanel Orientation="Horizontal" FlowDirection="RightToLeft" Grid.Row="1" >
              <Button Content="Cancel" HorizontalAlignment="Right" Width="60" />
              <Button Content="OK" HorizontalAlignment="Right" Width="60" />
            </StackPanel>
          </Grid>
        </ControlTemplate>
      </Setter.Value>
    </Setter>
  </Style>
</ResourceDictionary>

ProjectFoo

<Window x:Class="ProjectFoo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:controls="clr-namespace:ProjectBar.Controls;assembly=ProjectBar"
        Title="MainWindow" Height="200" Width="300">
    <controls:CustomControl>
        <Label>Place anything here</Label>
    </controls:CustomControl>
</Window>

, , ProjectFoo App.xaml

<Application x:Class="ProjectFoo.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:controls="clr-namespace:ProjectBar.Controls;assembly=ProjectBar"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <Style TargetType="{x:Type controls:CustomControl}">
            <Setter Property="Background" Value="Green"/>
        </Style>
    </Application.Resources>
</Application>

EDIT: ProjectBar AssemblyInfo.cs

[assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)]
0

, .

: ---- > Reference (Project Control) Project Consuming Any Cpu

"Control Project" "Any Cpu", " " x64, . ( )

0

All Articles