© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
7 replies
Mashmo

Inheriting a base window class in WPF

I have a couple of windows that use the same CloseWindow, MinimizeWindow, and WindowMouseDown functions that are implemented the same
This feels kind of a bad practice, so I tried implementing a base function for window that has all 3 of those functions and inheriting it in all of those windows
However it gave me a lot of problems in using the base Window functions (e.g. Show and Close) outside the class, and much more errors
Does anybody know a better way to address this problem?
This is the class I tried using as a base class:
namespace Client
{
    public abstract class TriviaClientWindow : Window
    {
        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                DragMove();
            }
        }
        private void CloseWindow(object sender, RoutedEventArgs e)
        {
            ((App)Application.Current).StopMusic();
            Thread.Sleep(50);
            Close();
        }
        private void MinimizeWindow(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }
    }
}
namespace Client
{
    public abstract class TriviaClientWindow : Window
    {
        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                DragMove();
            }
        }
        private void CloseWindow(object sender, RoutedEventArgs e)
        {
            ((App)Application.Current).StopMusic();
            Thread.Sleep(50);
            Close();
        }
        private void MinimizeWindow(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }
    }
}
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Find popup window in Wpf
C#CC# / help
2y ago
WPF Window Navigation
C#CC# / help
13mo ago