views:

392

answers:

2

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>
A: 

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.

kek444
The xaml code above is the entire content of the xaml file. I don't have a Generic.xaml. Don't I have the default style already? (<Style TargetType="{x:Type base:BaseWindow}">)Thanks!
Gustavo Cavalcanti
No, the generic.xaml is a predefined location where wpf looks up themes. You must have it in the Themes folder of your solution for default styles to work.
kek444
aaaahhh I did not know that. Thank you!
Gustavo Cavalcanti
Just by moving the code to a Themes folder and renaming the file to Generic.xaml didn't do anything... If I copy the entire ControlTemplate code to the Setter.Value it works. Why can't I do Setter Property="Template" Value="{StaticResource BaseWindowTemplate}"?
Gustavo Cavalcanti
Have you tried running the app? Sometimes the designer missess some stuff, but you still get the style applied at runtime.
kek444
Oh yes, Kek, I am trying to run the app.
Gustavo Cavalcanti
I was missing the ThemeInfo attribute in AssemblyInfo.cs - thanks for pointing that out!!!!
Gustavo Cavalcanti
+1  A: 

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.

Jobi Joy
Jobi, the my xaml file contains a resource dictionary. It lives in the custom control, not in my application. This is the template of the control, so the application must not know about it.
Gustavo Cavalcanti