Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a custom window:

C#
public class MyWindow : Window
{
   ...
}


and a separate style for this window:

XML
<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

1 solution

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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900