3

I have a base class (WPF window) the design (xaml) and code.

I would like to inherit this class, not in order to modify the design of this window, but only to modify the code.

For example, I have a log-in window which is my base class, it has a textbox for username, textbox for password and log-in button.

In addition it has the code that handles the login.

Now, I want to implement 2 base classes of this dialog:

  1. Authenticate against table X.

  2. Authenticate against table Y.

I thought that I can design my base dialog as follows:

... BaseLoginWnd : Window
{
    ...
    void Login_Click(...)  // This method is implemented as a Template Method (Design pattern)
    {
        ...
        if (Authenticate())
        {
            MessageBox.Show("Success");
        }
        else
        {
            MessageBox.Show("Failure");
        }
        ...
    }

    abstract bool Authenticate();
}

I'll mention that I know that this is not the best design for this particular example (It is better to create a separate class that performs the login), but this is just an example.

So my question is - How can I inherit from a window, using the base-class' design?

Matan
  • 680
  • 2
  • 14
  • 24
  • What have you tried? Have you seen [this question](http://stackoverflow.com/questions/11140000/creating-an-abstract-base-class-for-windows-in-wpf)? – Joe Amenta Apr 26 '14 at 13:11
  • 2
    The UI is not the proper place for authentication-related code. Remove all that from code behind and create a proper ViewModel and service layer and place every responsibility where it belongs. You don't need to subclass any UI elements for this because the UI has nothing to do with what you're doing here. – Federico Berasategui Apr 26 '14 at 14:24

1 Answers1

0

It is not possible to inherit a window/control class which has both XAML and code behind. Put the whole textbox/button creation logic in the code behind and remove the XAML file and then you will be able to inherit that class.

mca
  • 466
  • 3
  • 11