Gus
Gus

Reputation: 10659

WPF - Custom Window not working

I am trying to start building a Custom Window in WPF. I thought I had all the starting pieces in place, but so far, all I get is a regular Window with black content. I assume this is because it's not recognizing my template as the default one for the control.
Can you please let me know what I am missing? Here's my code:

namespace BaseWindowLibrary
{
    public class BaseWindow: Window
    {
        public BaseWindow()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(BaseWindow),
                                                     new FrameworkPropertyMetadata(
                                                        typeof(BaseWindow)));
        }
    }
}


<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:base="clr-namespace:BaseWindowLibrary">

    <ControlTemplate x:Key="BaseWindowTemplate" TargetType="{x:Type base:BaseWindow}">
        <Border BorderBrush="Blue" BorderThickness="3" Background="Coral" Width="100" Height="100"/>
    </ControlTemplate>

    <Style TargetType="{x:Type base:BaseWindow}">
        <Setter Property="Template" Value="{StaticResource BaseWindowTemplate}"/>
    </Style>

</ResourceDictionary>

Upvotes: 1

Views: 2069

Answers (2)

Jobi Joy
Jobi Joy

Reputation: 50038

Looks like you havent included the ResourceDictionary in to your application. Add it to the App.xaml

   <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="YourResource.xaml" />
   </ResourceDictionary.MergedDictionaries>

UPDATE based on the comment: I tried this BaseWindow:Window as a custom control and it just worked. The Style will be inside Generic.XAML of the custom control library.

Upvotes: 1

Kenan E. K.
Kenan E. K.

Reputation: 14111

Are you defining this xaml code in generic.xaml or in some other resource dictionary and then merging it in generic.xaml?

It's a requirement to have the style the default style.

Also, if you have been adding things by hand, check if VS aded the ThemeInfo attribute in AssemblyInfo.cs.

And if that doesn't work, you should post the code where you declare the window you use (the part in window.xaml or whichever name you use).

EDIT

To clarify, generic.xaml MUST be in the Themes folder of your solution and contain (directly or indirectly) the code for the style.

Upvotes: 3

Related Questions