Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF Style Window
Hi, I have a custom window:
 
public class MyWindow : Window
{
   ...
}
 
and a separate style for this window:
 
 
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:Component="clr-namespace:MyControls.Component">
 
    <!-- Style -->
    <Style x:Key="ExWindowStyle" TargetType="{x:Type Component:MyWindow}">
        <Setter Property="WindowStyle" Value="None" />
        <Setter Property="WindowState" Value="Maximized" />
    </Style>
</ResourceDictionary>
 
 
This code don't work and I have an exception because it can't create Component:MyWindow. Any ideas ? Thanks!
Posted 5-Feb-13 4:28am

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Your code post doesn't show enough detail to know what exactly you are doing and how but I can give you some general directions.
 
If you are creating a true custom control, then there is a lot of plumbing that is in the class which you skipped over in your post but which is key to making it all work. Specifically, the static class constructor needs:
 
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyWindow), new FrameworkPropertyMetadata(typeof(MyWindow)));
 
Without that, your class is still "Window" as far as the metadata goes. The xaml parsing engine can't match up the type declared in the xaml with the class type unless the class metadata is correct. But that is only the start for a custom control. You also need to override OnApplyTemplate and create some other boilerplate methods before you can even get to the point of writing your custom methods.
 
Secondly, for a custom control, your default style must be declared in themes/generic.xaml. Anywhere else and the parsing engine can't find it. The style declared in there cannot have an x:key. It will cause a parsing error. Further, you need to define a template or declare that the template is based on another template otherwise there will be no visual. Custom controls don't inherit the visuals of their parent. They are expected to be declared in the default style.
 
However, I'm curious why you need to create a custom descendent of a window when it seems all you are doing is overriding two properties. You would be WAY better off just creating a derived style and setting those two properties then applying by attaching to the Window class or explicitly specifying them on the windows they should apply to. The only reason to create a custom control is to add new behavior or otherwise modify existing behavior you can't otherwise get with the styling/templating features already in xaml.
 
If you have a good reason for a custom control, I'm about to publish a three-part series on writing custom controls in WPF. I can send you an advanced copy of the articles if you would like.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Marcin Kozub 265
1 Richard MacCutchan 239
2 OriginalGriff 208
3 Sergey Alexandrovich Kryukov 185
4 Praneet Nadkar 167
0 OriginalGriff 7,913
1 Sergey Alexandrovich Kryukov 7,232
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,865


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 5 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100