I have a custom window in WPF which I want to use as a base window for other windows.
When I tried to inherit it, I wrote in the XAML:
<my:MyWindow x:Class="NewWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:my="clr-namespace:MyNamesapce;assembly=MyAssembly"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
In the .cs code I wrote:
namespace SomeOtherNamespace
{
public partial class NewWindow: MyWindow
{
internal NewWindow(Control ctrl) : base(ctrl)
{
InitializeComponent();
this.ResizeMode = System.Windows.ResizeMode.NoResize;
}
}
}
But then I got the error:
cannot be the root of a XAML file because it was defined using XAML.
What am I doing wrong and how can I fix it?
If what you are trying to achieve is setting ResizeMode
to NoResize
in every window you could use a style like this:
<Style TargetType="Window" x:Key="windowStyle">
<Setter Property="ResizeMode" Value="NoResize" />
</Style>
Put this style in a ResourceDictionary and make it be the window style:
Style="{StaticResource windowStyle}"
But if you want to go further you'll have to make a new class inheriting from Window
public class MyWindow : Window
{
public MyWindow()
{
this.ResizeMode = ResizeMode.NoResize;
}
}
Now you are able to instanciate a new MyWindow
<mn:MyWindow x:Class="Project.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mn="clr-namespace:MyControls"
Height="300" Width="300">
</mn:MyWindow>
Be aware the class that will be the "code behind" of this new window need to inherit from your new MyWindow
class as below:
namespace Project
{
public partial class Window1 : MyControls.MyWindow
{
public Window1()
{
InitializeComponent();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With