Question

I'm using WPF with MahApps Metro controls.

I have created a base class for all my Window classes, that implements the INotifyPropertyChanged interface.

public class WindowBase : MetroWindow,INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public event PropertyChangingEventHandler PropertyChanging;

    protected virtual void RaisePropertyChanging(string propertyName)
    {
        PropertyChangingEventHandler handler = PropertyChanging;
        if (handler != null) handler(this, new PropertyChangingEventArgs(propertyName));
    }

    /* code ommited */
}

And I want to use it in my Window classes like so:

public partial class MainWindow : WindowBase

My XAML code to use MahApps MetroWindow:

<controls:MetroWindow ...

Which causes the following error:

Partial declarations of 'MainWindow' must not specify different base classes MainWindow.xaml.cs

So in order to use the INPC implemetations, in all my classes I need to find a way to inject those code to Window calsses in a way other that direct inheretance. Any ideas?

Was it helpful?

Solution

Code behind is partial declaration of your View and as evident from the error, you cannot specify different base classes for partial declaration of class.

In code behind you are deriving it from WindowBase and in xaml you are declaring it to be MetroWindow.

View declaration should be like <local:WindowBase.. where local will be namespace where WindowBase is declared ofcourse.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top