Question

I have a type of Window which I am using quite often (with an OK button and code-behind which only allows it to be enabled when other commands pass their validation checks). Instead of having to copy-paste this code-behind for each window I create which will have this, I would like to create this ValidOKWindow, which inherits Window and have all of my other windows inherit from it instead. However, I can't get this to work as-is.

I've tried creating ValidOKWindow as a child of Window and then substituting only the code-behind inheritance of its child classes:

public class WindowToInheritValidOK : ValidOKWindow

But the compiler hollers about the different base class in the .xaml (<Window>) which isn't suprising.

I've tried changing the base class in the .xaml as well

<ValidOKWindow x:Class="WPF.WindowToInheritValidOK"

But WPF apparently only allows its own classes as the base classes in the .xaml.

Since C# doesn't allow for multiple class inheritance, I don't know how to do this...

Was it helpful?

Solution

For any types outside of the basic framework itself which is included under the default xml namespace (xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation") you need to specify an xml namespace. For your own custom types in code this is usually done using an xmlns with clr-namespace. So if your Window classes are declared in a C# namespace "WPF" then that line should look like:

<local:ValidOKWindow x:Class="WPF.WindowToInheritValidOK" xmlns:local="clr-namespace:WPF"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top