Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding WPF deriving WIndow class

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...

public class MyClass : DerivedFromClass
{}

However, stumped in WPF and here's the issue. I want to build my own set of controls to be used as a baseline for a new learning project... preset my own styles, colors, backgrounds, and other functionality. No problem. Start first with a WPF Window and create "MyWindow".

Now, I want to take this baseline "MyWindow" and subclass THAT for yet another class of MySubClassedWindow. So, I create a new Window class, and by default, VS2010 builds the both designer and code portions of the form. I do view code on the MySubClassedWindow and find

partial class MySubclassedWindow : Window
{}

In C# using WinForms, I would just change to (and I've included the class library reference that includes the "MyWindow" declaration.

partial class MySubclassedWindow : MyWindow
{}

When I do, I get a compilation error of

Partial declarations of 'MyNameSpace.MySubclassedWindow' must not specify different base classes
like image 635
DRapp Avatar asked Aug 24 '11 10:08

DRapp


People also ask

Which is better WPF or UWP?

WPF is another UI framework, and UWP uses many concepts that you find in WPF, like XAML, data binding, styles etc. That means that a WPF developer gets up to speed with UWP quite fast, and vice versa. But WPF is not a UI framework that C++ developers can use, for WPF you have to develop with .

How many types of Windows do we use in WPF?

WPF offers three types of window you can use, each with its own strengths and associated costs. You build them using the Window class, the NavigationWindow class, and the Page class. I discuss them in the next sections. The Window object supports basic window functionality.

How do I know if a WPF window is open?

In WPF there is a collection of the open Windows in the Application class, you could make a helper method to check if the window is open. Here is an example that will check if any Window of a certain Type or if a Window with a certain name is open, or both. Show activity on this post. Show activity on this post.


2 Answers

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

<base:WindowBase x:Class="SubclassWindow.MainWindow"
                  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                  xmlns:base="clr-namespace:NamespaceForWindowBase"
                  Title="MainWindow" Height="350" Width="525">
    <!--...-->
</base:WindowBase>
like image 115
Fredrik Hedblad Avatar answered Sep 22 '22 06:09

Fredrik Hedblad


Having a base Window class brings a critical drawback, namely that binding to properties in your base class is much more difficult to do (and the currently accepted answer does not solve this problem). What's the point of inheriting if you cannot reference base properties? I have figured out how to set this up after some long hours, and wanted to share in the hopes that others will be spared this pain.

You may need to use things like value converters, which can only be referenced via static binding, which in my case made sense to have in the WindowBase class. I have included an example because I found it difficult to use these converters consistently in both design and run mode.

You cannot set the x:Name property of this inherited Window via XAML, but you may not need to do so if using the below approach. I have included an example of how to set the name, because inheriting from Window will not allow you to set the name at design time in the subclass. I do not recommend relying on the name of the window at design time, but setting d:DataContext should take care of any binding needs for you.

Be warned that in design mode, but not run mode, a copy of WindowBase (or the class specified in d:DataContext) will be instantiated in design mode and used as the binding context. So in very specific cases you may see data discrepancies, but in the vast majority of use cases this approach should suffice.

WindowBase.cs

````

public class WindowBase : Window
{
    //User-Defined UI Configuration class containing System.Drawing.Color 
    //and Brush properties (platform-agnostic styling in your Project.Core.dll assembly)
    public UIStyle UIStyle => Core.UIStyle.Current;

    //IValueConverter that converts System.Drawing.Color properties 
    //into WPF-equivalent Colors and Brushes 
    //You can skip this if you do not need or did not implement your own ValueConverter
    public static IValueConverter UniversalValueConverter { get; } = new UniversalValueConverter();

    public WindowBase()
    {
        //Add window name to scope so that runtime properties can be referenced from XAML
        //(Name setting must be done here and not in xaml because this is a base class)
        //You probably won't need to, but working example is here in case you do.
        var ns = new NameScope();
        NameScope.SetNameScope(this, ns);
        ns["window"] = this;

        //Call Initialize Component via Reflection, so you do not need 
        //to call InitializeComponent() every time in your base class
        this.GetType()
            .GetMethod("InitializeComponent", 
                System.Reflection.BindingFlags.Public | 
                System.Reflection.BindingFlags.NonPublic | 
                System.Reflection.BindingFlags.Instance)
            .Invoke(this, null);

        //Set runtime DataContext - Designer mode will not run this code
        this.DataContext = this;
    }

    //Stub method here so that the above code can find it via reflection
    void InitializeComponent() { }
}  

SubClassWindow.xaml

<local:WindowBase
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:YourProjectNamespace"
        x:Class="YourProjectNamespace.SubClassWindow"
        mc:Ignorable="d"
        d:DataContext="{d:DesignInstance Type= {x:Type local:WindowBase}, IsDesignTimeCreatable=True}"
        Title="SubClassWindow" Height="100" Width="300">
    <!--Design-time DataContext is set in d:DataContext. That option does not affect runtime data binding
        Replace local:WindowBase with local:SubClassWindow if you need to access properties in SubClassWindow-->
    <Grid Background="{Binding UIStyle.BackgroundColor, Converter={x:Static local:WindowBase.UniversalValueConverter}}"></Grid>
</local:WindowBase>

Nothing is needed in the SubClassWindow code behind (not even a constructor).

like image 37
marknuzz Avatar answered Sep 21 '22 06:09

marknuzz