Search code examples
c#wpfinheritancewindow

How can I access inherited members in a WPF window?


I am trying to create a base class for my windows in a WPF application but I cannot access their members in the derived classes. For example, here is the base window:

namespace MyApp.Windows
{
    public class BaseWindow : Window
    {
        public int MyProp { get; set; }
    }
}

And here is a window:

public partial class SomeWindow : BaseWindow
{
    public SomeWindow()
    {
        InitializeComponent();
        Loaded += SomeWindow_Loaded;
    }

    private void SomeWindow_Loaded(object sender, RoutedEventArgs e)
    {
        MyProp = do something;
    }
}

If I leave it like this, the MyProp proerty works just fine but I get an error that InitializeComponent() is not recognized. Therefore, in the window xaml I change the x:Class as follows: Before

<Window x:Class="MyApp.SomeWindow"

After

<Window x:Class="MyApp.Windows.BaseWindow"

Now, InitializeComponent() doesn't give me anymore issues but MyProp suddenly isn't recognized. Why?

In case it helps, what I want is to have all windows raise an event once they are loaded (the Loaded event is fired) and I don't want to write this code for every window I have, so I thought I could write this code in a base class, derive my windows from this and, well, have everything work.

Edit: Here's all the code. BaseWindow.cs (no other xaml):

using System.Windows;

namespace MyApp.Windows
{
    public class BaseWindow : Window
    {
        public int MyProp { get; set; }
    }
}

MainWindow.xaml.cs

namespace MyApp.Windows
{
    public partial class MainWindow : BaseWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

MainWindow.xaml:

<myapp:BaseWindow x:Class="MyApp.Windows.BaseWindow"
        xmlns:myapp="clr-namespace:MyApp.Windows"
        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:MyApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>

    </Grid>
</myapp:BaseWindow>

Solution

  • In order to change the base class of SomeWindow from Window to BaseWindow, you need to replace Window by BaseWindow wherever it occurs.

    So

    public partial class SomeWindow : Window
    

    becomes

    public partial class SomeWindow : BaseWindow
    

    and

    <Window x:Class="MyApp.Windows.SomeWindow" ...>
    

    becomes

    <myapp:BaseWindow x:Class="MyApp.Windows.SomeWindow"
                      xmlns:myapp="clr-namespace:MyApp.Windows" ...>
    

    with the unavoidable XAML namespace prefix.


    This is the BaseWindow class used in the example above:

    namespace MyApp.Windows
    {
        public class BaseWindow : Window
        {
            public int MyProp { get; set; }
    
            public BaseWindow()
            {
                Loaded += BaseWindow_Loaded;
            }
    
            private void BaseWindow_Loaded(object sender, RoutedEventArgs e)
            {
            }
        }
    }