Understanding WPF deriving WIndow class

前端 未结 2 1116
刺人心
刺人心 2020-12-01 12:43

I\'m sure this is easy, but new to me for WPF using C#. I know about inheriting from classes and have done so many times such as in C# WinForms projects...

         


        
2条回答
  •  旧时难觅i
    2020-12-01 13:24

    Your base class should just be a class file (not a Window).

    So create WindowBase.cs

    public class WindowBase : Window
    {
        // ...
    }
    

    In MainWindow (for example) change the xaml.cs file to inherit from WindowBase instead

    public partial class MainWindow : WindowBase
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        // ...
    }
    

    In MainWindow.xaml, include the namespace for WindowBase and change Window to base:WindowBase like this

    
        
    
    

提交回复
热议问题