Kevin
Kevin

Reputation: 41

C# - WPF - Changing the inheritance of my window

Hello StackOverflow community,

I got a question regarding the inheritance of the WPF windows.

If you create a new window it inherits from System.Windows.Window - so let's just say: I want that every window that I use have following code in his constructor:

MinHeight = Height;
MinWidth = Width;

So my first attempt was that I create a new class called BaseWindow and put the code block into the constructor of BaseWindow (this method I also often used for forms application and it worked there).

namespace MyProject.Classes
{
    public class BaseWindow : System.Windows.Window
    {
        public BaseWindow()
        {
            MinHeight = Height;
            MinWidth = Width;
        }
    }
}

My BaseWindow class is now existing and now I let my window wndLogin inherit from the BaseWindow - so something like that:

using MyProject.Classes;

namespace MyProject
{
    public partial class wndLogin : BaseWindow
    {

        public wndLogin()
        {
            InitializeComponent();
            InitializeWindow();
        }

        private void InitializeWindow()
        {
            // Initializing window specific stuff
        }
    }
}

I noticed that this class is partial, so I also changed the inheritance on the other part of the class in the file wndLogin.g.i.cs - it says there, that this file is an auto generated file and should not be edited.

namespace MyProject {
    /// <summary>
    /// wndLogin
    /// </summary>
    public partial class wndLogin : System.Windows.Window, System.Windows.Markup.IComponentConnector {
        // auto generated stuff here
    }
}

When I edit it anyway it replaces my custom inheritance with the standard inheritance → System.Windows.Window after debugging the project.


Is there a way to disable this automatic generated code or to tell Visual Studio that it should use an other class for inheritance?


Thanks in advance! :-)

Upvotes: 0

Views: 954

Answers (1)

Ron Beyer
Ron Beyer

Reputation: 11273

Don't hand-edit wndLogin.g.i.cs, it is generated automatically (as you found out).

In order to do this, you need to also change the XAML file to use your window:

<local:BaseWindow x:Class="MyNamespace.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MyNamespace"
        mc:Ignorable="d"
        Title="Window1">
    <Grid>

    </Grid>
</local:BaseWindow>

Notice the <local:BaseWindow ... instead of Window. If your BaseWindow class is in an entirely different namespace, you'll need to specify that in the xaml:

<baseWindowNs:BaseWindow x:Class="MyNamespace.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:MyNamespace"
        xmlns:baseWindowNs="clr-namespace:MyProject.Classes"
        mc:Ignorable="d"
        Title="Window1">
    <Grid>

    </Grid>
</baseWindowNs:BaseWindow>

The key line being the namespace qualifier: xmlns:baseWindowNs="clr-namespace:MyProject.Classes", and then use your namespace name in place of local from the first sample.

The only other change needed is the WindowName.xaml.cs file to inherit from the new base class:

/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : BaseWindow
{
    public Window1()
    {
        InitializeComponent();
    }
}

Alternatively (and more in-line with WPF MVVM conventions) you could create a base view model that has those properties and bind your window class Height and Width to those viewmodel properties. Personally that is how I would approach this rather than create a new base class for windows.

Upvotes: 3

Related Questions