Search code examples
wpfsubclassing

Subclassing WPF Window


I created a WPF Window

than i modified its class definition to:

public partial class myWindow : mySubclassedWindow

compiler throws:

"Partial declarations of 'myWindow' must not specify different base classes"

myWindow.Xaml:

x:Class="WpfGridtest.myWindow"

so somewhere, there is another partial class, that inherits from "Window" but i cannot find it. How can i override my case to use subclassed window?


thanks Jon, that was the problem. also found this helpful article: Link


Solution

  • That would be in the declaration of myWindow itself - the designer will be generating the other half of the partial type based on the XAML, based on your element type.

    You can use an element of <mySubclassedWindow> instead, so long as you give it the appropriate namespace and assembly references.

    EDIT: Okay, so here's a short example, in a project called WpfApplication. My Window subclass:

    using System.Windows;
    
    namespace WpfApplication
    {
        public class EnhancedWindow : Window
        {
        }
    }
    

    My XAML:

    <y:EnhancedWindow x:Class="WpfApplication.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:y="clr-namespace:WpfApplication"
            Title="MainWindow" Height="350" Width="525">
    </y:EnhancedWindow>
    

    My partial type:

    namespace WpfApplication
    {
        public partial class MainWindow : EnhancedWindow
        {
            public MainWindow()
            {
                InitializeComponent();
            }
        }
    }
    

    It all builds with no problem. I think that's what you wanted to do, right?