0

I'd like to my own basic MainWindow. I'd than create a new MyWindow which inherits all the controls and stuff from custom MainWindow.

I tried to google it but I haven't found answer to my problem. When I try to use a new Window as base for another Window Visual Studio says:

WpfApplication1.MainWindowCustom cannot be the root of a XAML file because it was defined using XAML.

How can I achieve this?

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
daralim
  • 183
  • 10

1 Answers1

1

Base Windows can only be created using code, not XAML.

So basically this is what you have to do:

public class BaseWindow : Window
{
    // custom logic here
    // add controls, etc.
}

Then you derive from that BaseWindow in every other Window:

<local:BaseWindow x:Class="SomeWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:Namespace.To.Local"
>
</local:BaseWindow>

And:

public partial class SomeWindow : BaseWindow
{
}
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325