Search code examples
wpfclassradix

Using Base classes in WPF


I have problem with base classes in WPF. I try to make a base class with some base elements, so that other windows can inherit these components. But all that i have, when I inherit base class is only empty window, without these elements. For better understanding i put my code here:

using XSoftArt.WPFengine;

namespace XSoftArt
{

    public class WindowBase : Window
    {

    public WindowBase()
    {

    }

}

Code of the Windows, whitch inherits WindowBase:

namespace XSoftArt.WPFengine
{
    public partial class NewAbility : WindowBase
    {
        public NewAbility()
        {
            base.ChildForm = this; InitializeComponent();

        }
    }
}

Or maybe someone can put an working example or link with implemented base classes in wpf? Thanks


Solution

  • I don't think I'd ever use inheritance in WPF the way you're trying to use it.

    I'll try and take a stab at answering your question. If I'm understanding you correctly, you're trying something like this:

    1. You're creating a window that has both a XAML file and a code-behind.
    2. You're adding "base elements" to the XAML for your window... I'm not sure what you mean by "base element", but I'm going to assume you mean you're adding UI elements to your window.
    3. You're creating another window that "derives" from your first window in the code-behind, and the problem is that you're not seeing the UI elements on it from your "base" window.

    If that is what you want to accomplish with WPF, I'd personally recommend against it, just because I'm personally not a fan of inheritance and have seen firsthand the dangers of letting inheritance get out of hand.

    What you could try instead is organize your "base" UI elements into WPF UserControls. This tutorial might be able to guide you in the right direction. Good luck!