WPF: Inheriting from custom class instead of Window

In ASP.NET, we learned that it is often interesting to inherit from another class than from System.Web.UI.Page. This allows to define common methods, such as utilities, etc... which are used by a set of web pages throughout an application.
In WPF, it's also possible to do the same, and to inherit from a custom class instead of System.Windows.Window, of System.Windows.Controls.Page, or of System.Windows.Controls.UserControl for example.
When you add a Window (or Page, or UserControl...) to a WPF project, the chain of inheritance is as follows:
Simple inheritance
If you have a special method "doSomething()" which you want to reuse in every Window in the application, then you can store it in an abstract class GalaSoftLb.Wpf.WindowBase and modify the inheritance as follows:
Custom inheritance
In the diagrams above, the Window class is the framework's one, and the class GalaSoftLb.Wpf.WindowBase is abstract.
In order to get the new Windows to derive from this abstract class, not only the C# code must be modified, but also the XAML code. This is a little tricky, because a special syntax must be used. Instead of the usual:
<Window x:Class="WindowsApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WindowsApplication1" Height="300" Width="300" > <Grid> </Grid> </Window>
We have instead:
<src:WindowBase x:Class="WindowsApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:src="clr-namespace:GalaSoftLb.Wpf" Title="WindowsApplication1" Height="300" Width="300" > <Grid> </Grid> </src:WindowBase>
Instead of deriving from Window, we derive from WindowBase. However, WindowBase is in an unknown namespace, so we must tell the XAML parser where to look for this class. This is done using the "xmlns:src" line. "xmlns" is for "xml namespace". "src" is a prefix, you are free to choose any qualifier you want. Typically, we use "src" for source, or "clr" for common language runtime, but this is up to you. It must be unique in this XML document.
Instead of specifying a URL like we normally do in XML, we use the syntax "clr-namespace" to define the src namespace. This means that the XAML parser will look into the Common Language Runtime, and look into a namespace named GalaSoftLb.Wpf to find the class WindowBase.
Note: The XML namespace and the CLR namespace are two different notions, we are at the crossroad of two worlds here. It's important to understand what these two namespaces represent.
Often, the base class is stored in an external class library, in order to reuse it in different WPF applications. In this case, the syntax becomes:
<src:WindowBase x:Class="WindowsApplication2.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:src="clr-namespace:GalaSoftLb.Wpf;assembly=GalaSoftLb.Wpf" Title="WindowsApplication2" Height="300" Width="300" >
Here, we tell the XAML parser to look for the namespace GalaSoftLb.Wpf into the DLL GalaSoftLb.Wpf.DLL, which must be referenced in the project.
In the code behind, there are no surprises:
public partial class Window1 : GalaSoftLb.Wpf.WindowBase
Finally a note about the implementation: If you want to store the abstract WindowBase class in a class library, you must add the following .NET libraries to the list of References: PresentationFramework, PresentationCore, WindowsBase.
Print | posted on Friday, March 2, 2007 10:20 AM

Feedback

# re: WPF: Inheriting from custom class instead of Window

left by Ini at 6/14/2007 3:47 AM Gravatar
Thanks for this great article. Very useful.

Can't view the form in Design mode after implementing a custom class. (VS 2005 with .NET 3.0 extensions)

Getting the following error message:

Window must be the root of the tree. Cannot add Window as a child of Visual.

Am I missing anything?

Thanks,
I.

# re: WPF: Inheriting from custom class instead of Window

left by Nino at 7/2/2007 8:53 PM Gravatar
Thanks for the hint, it helped me a lot.
Just to point to the problem I had:
I was unable to inherit my custom window defined in the application itself until I've successfully built the application with the custom class, and then inherit it.

# re: WPF: Inheriting from custom class instead of Window

left by Lori at 7/11/2007 11:35 AM Gravatar
I'm basically doing the same thing. I've retrofitted a Window to derive from a "WindowBase" class so that I can reuse some public properties and methods. When I run, I receive no errors, but I get a black screen. Do you have any pointers?

# re: WPF: Inheriting from custom class instead of Window

left by Laurent at 7/11/2007 7:22 PM Gravatar
Unfortunately, without more details, it's impossible to tell you why this happens. Try to make the simplest possible repro of that problem, zip all the files and send them to me, I will look into it. Have some patience though, as I am leaving for 2 weeks holidays soon.

Greetings,
Laurent

# re: WPF: Inheriting from custom class instead of Window

left by Emmanuel Huna at 7/18/2007 7:04 AM Gravatar
I followed your example but I get:

'TestInherit.WindowBase' cannot be the root of a XAML file because it was defined using XAML. Line 2 Position 5.

I have one window named "WindowBase.xaml" with a property named "TestProperty".

I have another window named "WindowDerived.xaml" where I changed the XAML to:

<src:WindowBase xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:TestInherit"
x:Class="WindowDerived"
Title="TestInherit" Height="407" Width="300"
>
<Grid>
</Grid>
</src:WindowBase>

Any ideas why I'm getting this error?

# re: WPF: Inheriting from custom class instead of Window

left by Laurent at 7/28/2007 3:21 AM Gravatar
Hi,

I am not sure what went wrong, but I am also not sure what exactly you're doing. Same proposal as with the previous poster, send me a zip with your code and I'll look at it.

Greetings,
Laurent

# re: WPF: Inheriting from custom class instead of Window

left by Bruce Philp at 9/16/2007 3:37 AM Gravatar
Hi,

I have tried following your example, but get the same error in "cannot be the root of a XAML file because it was defined using XAML".

Is there any chance you could send the example so that i can see what i cam doing or make the code available?

Many Thanks,
Bruce Philp

# re: WPF: Inheriting from custom class instead of Window

left by Ed Noepel at 10/2/2007 5:46 AM Gravatar
I would love to see the code as well. From my experience, you cannot subclass one element defined with XAML from another.

# re: WPF: Inheriting from custom class instead of Window

left by Laurent at 10/2/2007 9:21 AM Gravatar
Ed, I sent you a code example. I am not sure what you mean with "one element defined with XAML from another." In the example here, the class WindowBase does not contain XAML, it's just a class deriving from System.Windows.Window.

Note also that if the WindowBase class is abstract, Blend cannot handle it, so it's better to make it non-abstract.

Hope that clarifies things,
Laurent

# re: WPF: Inheriting from custom class instead of Window

left by Ed Noepel at 10/2/2007 2:29 PM Gravatar
I just felt it was important you mention the base window is not a "normal" WPF window defined using XAML with a code-behind. If it was, you would get the "cannot be the root of a XAML file because it was defined using XAML" compile-time error when trying to subclass it.

IMHO, this is a shortcoming of WPF. The workaround I am currently using involves moving all the XAML from the base window into a ControlTemplate and storing it in a ResourceDictionary. With this workaround, binding UIElement events in the template to handlers in the base window code is a bit obtuse. But it works.

# re: WPF: Inheriting from custom class instead of Window

left by Laurent at 10/2/2007 7:37 PM Gravatar
Ed,

If I understand your comment correctly, what you are trying to do is one of the big no-nos in WPF: Subclassing XAML markup. The regretted Kevin Moore (no, he didn't pass away, he just left Microsoft ;) has a post about this:

http://work.j832.com/2007/06/don-subclass-panel-unless-you-making.html

I think that you'll find in this post the answers to your problem.

The WindowBase in my example is a "normal" WPF window. In fact, when you define a Window (or any other UI element) in XAML, you actually just write the serialized version of this CLR object (XAML is a serialization language, not just a UI markup language). The only "shortcoming" of WPF (if you really want to name it so) is that you should not use subclassing to create a predefined look and feel for your UI elements. In fact, this is what User Controls are for, so I encourage you to use them to do what you want to achieve.

If I misunderstood your point, forgive me :-)

Greetings,
Laurent

# re: WPF: Inheriting from custom class instead of Window

left by Jonathan at 10/3/2007 5:36 AM Gravatar
When I try this, I get errors:
Partial declarations of 'InfinID.UI.Win.MyTest' must not specify different base classes

Apparantly there is a code generated .g.cs version of my class created that is inheriting the Window class, while my .cs partial class implements my WindowBase custom class. I can manually change the .g.cs file, but that is kind of a pain for every window I create.

Anyone else experience this?

Jonathan

# re: WPF: Inheriting from custom class instead of Window

left by Jonathan at 10/3/2007 5:52 AM Gravatar
Nevermind. I missed part of the xaml syntax. Nice article!

# re: WPF: Inheriting from custom class instead of Window

left by Laurent at 10/3/2007 5:54 AM Gravatar
Hi,

This is a symptom that you changed the base class in the XAML and forgot to change it in the code-behind, or the contrary (changed in the code behind and forgot to change it in the XAML). The class specified in the XAML and in the code behind must match.

You must NOT modify the .g.cs file, because it is generated (that's what ""g" stands for). Your changes will be overwritten every time you build your project.

HTH,
Laurent

# re: WPF: Inheriting from custom class instead of Window

left by Dmitriy at 10/9/2007 6:18 PM Gravatar
Hi Laurent!

This is great, that I have found your article! Wery good starting point for my testing so called "XAML + C#". :-)

I have also found a wounder, that Blend cannot handle abstract class. And one more point.

Why can I not do something like this:

<src:Display x:Class="Application1.Window1"
...
MouseLeftButtonDown="DragAttempt">

By running InitializeComponent(), I become an exception, that 'DragAttempt' value cannot be assigned to property 'MouseLeftButtonDown' of object 'Application1.Window1'. Error binding to target method.

If i do this in code like
this.MouseLeftButtonDown += new MouseButtonEventHandler(DragAttempt);
all works properly. Wery interesting. All works also by deriving from Window.

I have much more problems with events in WPF. I have seen it many times and it has happened to me where events just dont get raise. For examlpe this "MouseLeftButtonDown" never fires, in my application, when "linked" to button event button1.MouseLeftButtonDown, but fires, when "linked" to window, for example (see above). Have you ever seen such problems? I do something wrong, or this events is really can be lost? :-)

Have a nice day and good luck in Your work!

Dmitriy.

# re: WPF: Inheriting from custom class instead of Window

left by Bob Binette at 11/12/2007 5:52 AM Gravatar
The example work find with Visual Studio 2005 SP 1 + WPF extensions but it doesn't work with Visual Studio 2008 beta 2. For more details :

WPF (XAML) Visual Inheritance
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=289715

Bob

# re: WPF: Inheriting from custom class instead of Window

left by Laurent at 11/12/2007 7:09 AM Gravatar
Hi Bob,

No, you misunderstood my example. I am not doing XAML inheritance. The class WindowBase is a pure C# class.

What the "bug" in Connect describes is known as "Markup subclassing" and is explicitly warned against by Microsoft.
http://work.j832.com/2007/06/don-subclass-panel-unless-you-making.html

Greetings,
Laurent

# re: WPF: Inheriting from custom class instead of Window

left by James Fisk at 12/17/2007 3:58 AM Gravatar
Thank you, this is just what I've been looking for.

# re: WPF: Inheriting from custom class instead of Window

left by Thomas Hetzer at 1/11/2008 5:21 AM Gravatar
Thanks for sharing your code - it's a great convenience.

# re: WPF: Inheriting from custom class instead of Window

left by jc at 1/31/2008 5:40 AM Gravatar
Great article! Worked perfectly. I tried to take it one step further - create a defaultpage.xaml file that inherits by pagebase.cs custom control and then inherit the defaultpage.xaml for all other xaml pages i create. This doesn't work. Ouch!

Is there a way around this besides styles and themes that I'm not understanding? Why can't you inherit a xaml file that is created declaritively?

# re: WPF: Inheriting from custom class instead of Window

left by Laurent at 1/31/2008 5:48 AM Gravatar
Hi jc,

I am not sure I understand what you are trying to do.

Greetings,
Laurent

# re: WPF: Inheriting from custom class instead of Window

left by Richard King at 2/12/2008 2:46 AM Gravatar
Fantastic !

I spent days trying to figure out how to get around the .g generated file reverting to the standard UserControl base class
when it regenerates. It seemed as if you couldn't use a custom base class in a user control that also had XAML.

So glad I found this post. Thanks

# re: WPF: Inheriting from custom class instead of Window

left by Prakash at 2/19/2008 11:50 AM Gravatar
Having the abstract class, the designer never works/displays the actual UI. I was getting "Unable to create instance of <type>". Instead of having a abstract class, just have a normal class that inherits the Window. This way everything works cool!

# re: WPF: Inheriting from custom class instead of Window

left by Laurent at 2/19/2008 8:25 PM Gravatar
Hi Prakash,

Yes, this is one of the most annoying limitations of Blend (and I guess of Cider too): They are unable to display abstract classes. I dislike making WindowBase non-abstract, because it goes against good practice. WindowBase should not be instantiated, period.

I already informed the Blend team some time ago about this fact, but I doubt that they will change it.

Thanks for your feedback
Laurent

# re: WPF: Inheriting from custom class instead of Window

left by sever_01 at 3/3/2008 3:56 AM Gravatar
How about inheriting from generic classes?

Can I use such base class:
class BaseWindow<T>:Window
{...}

# re: WPF: Inheriting from custom class instead of Window

left by Laurent at 3/4/2008 7:42 PM Gravatar
@sever_01 XAML doesn't like generics, so you won't be able to use such a BaseWindow<T> class in your XAML file. If you do code-behind only, then it should work.

HTH

# re: WPF: Inheriting from custom class instead of Window

left by erked at 3/5/2008 4:36 AM Gravatar
Yes, thank you; very useful and a time/life saver.

ESD

# re: WPF: Inheriting from custom class instead of Window

left by Pouyan at 4/27/2008 6:49 AM Gravatar
Dear Laurent

Thank you for your website which helps me a lot.
I have a very bad problem with pages and frames.
I create an xbap application with a Main Page and several pages in Frames. I have access to all controls in pages through frames but I am not able to have access to controls in Main Page through pages in frames.
I would be so grateful if you could you help me and give me a solution.
Best Regards,
Pouyan

# re: WPF: Inheriting from custom class instead of Window

left by The Cite at 6/25/2008 6:03 AM Gravatar
I enjoyed your article alot. I have one problem - how can I create controls on the Base Form for all forms to inherit w/o Xaml.

Furthermore, why did the Microsoft team make it that U can't inherit from a class defined in Xaml?

# re: WPF: Inheriting from custom class instead of Window

left by Dean at 7/22/2008 9:54 PM Gravatar
I can't seem to apply this logic to my situation.

I have a C# class which inherits from UserControl - 'BaseControl'. I build the assembly as 'MyControls.BaseControls'.

namespace MyControls
{
public class BaseControl : UserControl
{
public BaseControl(){}

public static DependencyProperty TestingProperty = DependencyProperty.Register("Testing", typeof(string), typeof(BaseControl));
public virtual string Testing
{
get { return (string)GetValue(TestingProperty); }
set { SetValue(TestingProperty, value); }
}

}//END CLASS
}//END NAMESPACE


In another WPF UserControl assembly 'MyControls.TestingControls' I reference the above assembly - 'MyControls.BaseControls'. I inherit this new control from 'BaseControl', as such:


namespace MyControls
{

public partial class TestBox : MyControls.BaseControls.BaseControl
{
public TestBox()
{
InitializeComponent();
}
}//END CLASS
}//END NAMESPACE

My Xaml looks like the following:

<NB:BaseControl x:Class="FR.Library.Controls.TestBox" x:Name="TB"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:NB="clr-namespace:MyControls.BaseControls;assembly=MyControls.BaseControls"
Height="300" Width="300">

<Grid>

</Grid>
</NB:BaseControl>


When I build 'MyControls.TestingControls' I receive the following error:

The tag 'BaseControl' does not exist in XML namespace 'clr-namespace:MyControls.BaseControls;assembly=MyControls.BaseControls'.

What the heck am I doing wrong?

Thanks.

# re: WPF: Inheriting from custom class instead of Window

left by Laurent at 7/25/2008 4:36 AM Gravatar
Dean,

Zip and email me the project files, I'll take a look.

Laurent

# re: WPF: Inheriting from custom class instead of Window

left by Laurent at 7/27/2008 4:30 AM Gravatar
Hi,

You are not following my example but instead you are doing what I mention (a little later in the comments) as the big no-no of WPF. Check this article:

http://work.j832.com/2007/06/don-subclass-panel-unless-you-making.html

You can't subclass a control defined in XAML.

Greetings,
Laurent

# re: WPF: Inheriting from custom class instead of Window

left by Dave Rigsby at 7/27/2008 6:32 AM Gravatar
Ah, I see. That kind of blows since you seem to lose a lot of the functionality/features that drove the decision to use WPF in the first place. My problem is that I have a set of WPF User Controls for which I would like provide some "base functionality" so as to remove the need to duplicate code.

In addition, the "visual editor" does not seem to like WPF User Controls that inherit from "regular" user controls. So, it seems that I would be SOL as to visual design (?).

I just have to wonder why the devs decided that inheriting from a WPF User Control should be restricted. Granted, I am new to WPF but I just do not see the wisdom in some of these limitations. I can see now that learning WPF to any degree of comfort is going to be painful :)

Thanks again for your tutorial and insight!

# re: WPF: Inheriting from custom class instead of Window

left by Laurent at 7/27/2008 7:47 AM Gravatar
Hi,

Base functionality can be provided by implement a CustomControl, this is actually the recommended way to do that in WPF. CustomControls can provide a default template for look&feel, and their template can be modified at will.

This is actually the reason why you don't really need to subclass a UserControl in XAML. Instead, create a CustomControl with the desired functionality and use different templates to make the new control look differently. For example, did you know that a CheckBox, a RadioButton, a RepeatButton, a ToggleButton all share the same basis class? It's because a CheckBox and a ToggleButton are actually the same thing, but they just look differently.

WPF is hard to learn because it breaks away from many things that we learned in WinForms and other classic technologies. But with time, I hope you'll agree with me that it is actually a better way to do things.

I don't understand what you mean with the "visual editor". Do you mean Cider (in VStudio) or Blend? And what exactly is failing there?

Thanks,
Laurent

# re: WPF: Inheriting from custom class instead of Window

left by Dave Rigsby at 7/27/2008 7:14 PM Gravatar
The Visual Studio 2008 Visual Designer did not seem to like WPF User Controls that inherit from "regular" user controls. I received a message reading, "The document root element is not supported by the visual designer".

Getting back to the original post, I have redesigned by classes to work around the problem. I guess I need to buy few good books on WPF :) I guess the key is to stop thinking in "Winform-ese".

An article I read some time ago advised something to the effect of "In order to learn WPF, you must first forget everything you know about winforms." I see now that this is fairly sage advice :)

# re: WPF: Inheriting from custom class instead of Window

left by CF at 7/28/2008 6:33 PM Gravatar
Merci !

Très pratique !

CF

# re: WPF: Inheriting from custom class instead of Window

left by worapot at 8/7/2008 4:18 PM Gravatar
from error
cannot be the root of a XAML file because it was defined using XAML. Line 2 Position 5.

Send Email project example of Inherit WPF

Thank Laurent

Software_Intelligence@hotmail.com or
worapot@prosoft.co.th

# re: WPF: Inheriting from custom class instead of Window

left by chrisd at 9/4/2008 6:14 PM Gravatar
I've been playing with using Vista TaskDialogs in WPF. After a lot of work, I can display them, but only using code. It would be much nicer to be able to use them in markup.

I took the "let's just try this" approach and derived my TaskDialog class from WindowBase as above, and it works--but the main thread of the app never terminates. When I close the main window, it closes, but the process remains alive.

Do you have any thoughts on why this is and how my goal might be accomplished?

# re: WPF: Inheriting from custom class instead of Window

left by chrisd at 9/10/2008 1:56 AM Gravatar
Just for closure, I have found that the non-termination referenced in my previous post was caused by something unrelated.

No luck using TaskDialogs from markup, though.

# re: WPF: Inheriting from custom class instead of Window

left by Marco Castro at 10/8/2008 12:13 AM Gravatar
Hi,

I got the same problem of other users: "'Inheriteds.BasicPage' cannot be the root of a XAML file because it was defined using XAML. Line 1 Position 17." Can you send me a working version of it?

<src:BasicPage x:Class="WPFTest.UserPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:Inheriteds;assembly=Inheriteds"
Title="UserPage">
<Grid>
<Label Height="53" Margin="56,0,12,12" Name="label1a" VerticalAlignment="Bottom" FontSize="32">Test</Label>
</Grid>
</src:BasicPage>

Basic page is a simple Window in another assembly.

Thanks,
Marco Castro

# re: WPF: Inheriting from custom class instead of Window

left by Đoàn Mạnh Hà at 11/14/2008 3:48 AM Gravatar
that is all i need, thanks!

# re: WPF: Inheriting from custom class instead of Window

left by Gabriel Armando Prieto at 12/11/2008 10:05 PM Gravatar
for issue "Can't view the form in Design mode after implementing a custom class." simply remove "abstract" modifier to WindowBase and then you will see your window in design mode and work at the same time.

Live VERY Long and Prosper... :)

# re: WPF: Inheriting from custom class instead of Window

left by Ashish Sehajpal at 1/5/2009 12:27 PM Gravatar
Hi Laurent,

i am facing a problem while inheriting from my custom window. main aim is to put the basic design on this custom window and then inherit it on every other window. here is the structure of custom window.

-----------------------
LOGO | BUILD VERSION
-----------------------



-----------------------
FOOTER CONTROL
-----------------------

When i inherit from it, the design is visible on the child window. But if i put some controls on child window, whole of the design gets washed away. what i got is just a white background.

can you help me out why it is happening ?

# re: WPF: Inheriting from custom class instead of Window

left by Laurent at 1/6/2009 10:28 PM Gravatar
Hi Ashish,

This is not how we do things in WPF. You must think a little more as a web designer in this technology. What you try to do is a little like a masterpage in ASP.NET. Define a Grid in your main window. First row and last row of the grid are your header and footer. In the middle, place a Border (for example) which will be the container for your content.

Then, define the content in UserControls. Each user control is loaded by the main window and placed in the Border. This way you can even implement a kind of navigation system, and the state is kept by the main window.

Does this make sense?

Cheers,
Laurent

# re: WPF: Inheriting from custom class instead of Window

left by Ashish Sehajpal at 1/7/2009 5:28 AM Gravatar
Thanks for your kind reply Laurent!

Issue is not about what we do or not here. This is the architecture our client recommends. We already have all the usercontrols, custom controls designed for the system. we used in the same way as you suggested. but the concept of putting design elements (like border->scrollviewer->border->grid-> [row1->logo & uild number, row2->content, row3->footer]) on every window is not acceptable to client.

what we need is to design a common window having all the design elements and logo+build_no+footer on it. and wherever we need these tings, we just need to inherit that window. period.

i know it may sound evil to you but this si the requirement here.

- A

# re: WPF: Inheriting from custom class instead of Window

left by Kamran at 2/7/2009 5:54 AM Gravatar
Hi,
I did every thing you said in this wonderful article, I don't have any error and my application Compiled successfully, but I have a problem, my window in design time
not appear and show this message: Problem loading : the document contain errors that must be fixed before the designer can be loaded...

# re: WPF: Inheriting from custom class instead of Window

left by Kamran at 2/7/2009 6:03 AM Gravatar
<src:BaseWindow x:Class="LearningWpf.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:LearningWpf"
Title="WPF Layout" Height="302" Width="337">

</src:BaseWindow>

# re: WPF: Inheriting from custom class instead of Window

left by Kamran at 2/7/2009 6:24 AM Gravatar
I change "BaseWindow" To "WindowBase" and add assembly reference to xmlns:src this message disappear but I can't see window elements in design mode this message shown: "Intentionally Left blank, the document root element is not supported by the visual designer."
how Can I see Window controls?

# re: WPF: Inheriting from custom class instead of Window

left by kamel at 2/27/2009 5:27 AM Gravatar
Please,

can you modify the article to highlight from the start that "the class WindowBase does not contain XAML, it's just a class deriving from System.Windows.Window."

Your article is very interesting and it's the only one I found around that explain inheritance with WPF.

However, I think that this little modification could save a lot of time for future readers.

Cheers,
Kamel

# re: WPF: Inheriting from custom class instead of Window

left by Rajesh Rathi at 6/4/2009 5:33 PM Gravatar
Excellent Article..
Cheers

# re: WPF: Inheriting from custom class instead of Window

left by Arthur at 6/25/2009 2:45 AM Gravatar
very helpful
thanks

# re: WPF: Inheriting from custom class instead of Window

left by Jay at 7/9/2009 4:10 PM Gravatar
I was receiving "Inconsistent accessibility: base class <Class Name Base> less accessible than <Class Name>" Just make sure you have Public class.

Also, as much as I would like to make the Base abstract I see you talk about how you would to, but also mention that it should never be instantiated.

However, doesn't <src:WindowBase></src:WindowsBase> instantiate an instance of the base class?
When I made my class abstract. It said...
Error 1
Could not create an instance of type 'WindowBase'
Anyway this example did help me a lot, Great work!

# re: WPF: Inheriting from custom class instead of Window

left by Charles Churchill at 8/6/2009 3:44 AM Gravatar
I couldn't get abstract to work for what I wanted, but this article was exactly what I was looking for.
Much thanks!
Charles

# re: WPF: Inheriting from custom class instead of Window

left by Ganesh at 8/19/2009 6:24 AM Gravatar
hello,

i tried this example it works great , one thing though can i try this example where in i have to have a few common buttons and controls in the base window and use those elements in the derived windows.

Can you give me your thoughts and suggestions as to whether this is a good idea.

# re: WPF: Inheriting from custom class instead of Window

left by Musa Doğramacı at 9/18/2009 7:58 AM Gravatar

2 wpf windows
WindowBase.cs
Window1.cs

for Window1:WindowBase:

XAML:
<WpfApplication1:WindowBase x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfApplication1="clr-namespace:WpfApplication1"
Title="Window1" Height="300" Width="300">
<Grid>

</Grid>
</WpfApplication1:WindowBase>

CodeBehind:

public partial class Window1 : WindowBase
{
public Window1()
{
InitializeComponent();
}
}

errors :

Warning 1 'WpfApplication1.Window1.InitializeComponent()' hides inherited member 'WpfApplication1.WindowBase.InitializeComponent()'. Use the new keyword if hiding was intended. C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\WpfApplication1\WpfApplication1\obj\Debug\Window1.g.cs 48 21 WpfApplication1

Error 2 'WpfApplication1.WindowBase' cannot be the root of a XAML file because it was defined using XAML. Line 1 Position 29. C:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\WpfApplication1\WpfApplication1\Window1.xaml 1 29 WpfApplication1


FIX:

make in WindowBase property panel set BuildAction property = Resource

Good works...

# re: WPF: Inheriting from custom class instead of Window

left by ibrahim at 10/7/2009 1:06 AM Gravatar
hello Laurent.. i am new to WPF... as you said WindowBase is just a class and derived from System.Windows.Window... I have created a class library with class named
class MyBaseWindow : System.Windows.Window
{
}
it gives me an error like...
Error 1 The type 'System.Windows.DependencyObject' is defined in an assembly that is not referenced. You must add a reference to assembly 'WindowsBase, Version=3.0.0.0, Culture=neutra..

any help will be appriciated..

Thanks

# re: WPF: Inheriting from custom class instead of Window

left by Ibrahim at 10/7/2009 1:18 AM Gravatar
ok.. igot the windowBase assembly in .net components

# re: WPF: Inheriting from custom class instead of Window

left by Marco Castro at 10/7/2009 10:10 AM Gravatar
Well fellows,

I concluded that this implementation works only in VS 2005 it does not work with VS 2008.

Thanks

# re: WPF: Inheriting from custom class instead of Window

left by Tin Le at 11/10/2009 12:27 AM Gravatar
I can not design on the class inherit from WindowBase. How to solve this problem bro!!!

# re: WPF: Inheriting from custom class instead of Window

left by Benjamin at 1/20/2010 2:05 AM Gravatar
Hello,

This works great with Visual Studio 2008! But there is no longer Intellisense in the <src:WindowBase> element. Do you have any clue to enable Intellisense in the inherited class ?

Thanks!

# re: WPF: Inheriting from custom class instead of Window

left by macias at 5/21/2010 12:22 AM Gravatar
Could you please put as an example your abstract class? My final XAML is like yours, however I get error:

Error 1 'WpfControlLibrary1.MyWindowX' cannot be the root of a XAML file because it was defined using XAML. Line 1 Position 14. F:\Projekty\WpfApplication1\WpfApplication1\Window1.xaml 1 14 WpfApplication1

Which is pretty sane, because first I reference the unknown tag, and later I define it.

# re: WPF: Inheriting from custom class instead of Window

left by Jeon at 5/26/2010 12:18 AM Gravatar
hi~

Give me Sample Plz!~! Email

Thank u

# re: WPF: Inheriting from custom class instead of Window

left by Jpeters at 8/30/2010 10:35 AM Gravatar
Thanks for this post I was able to use your pattern to decorate a usercontrol and include it within XAML

# re: WPF: Inheriting from custom class instead of Window

left by Roy T. at 11/21/2010 10:54 PM Gravatar
Thanks for this great sample, told me exactly what I wanted to know. I did almost miss the xml namespace declaration "xmlns:src="clr-namespace:My.NameSpace" so I did get a 'src' prefixed not defined error, but once I read again I spotted it.

Many thanks!

# re: WPF: Inheriting from custom class instead of Window

left by programmerInProblems at 3/31/2011 11:45 PM Gravatar
great stuff, it works perfect with windows, but it doesn´t work in pages (even with BuildAction property = Resource), anyone have and idea to resolve this

thanks

# re: WPF: Inheriting from custom class instead of Window

left by caveman at 11/22/2011 10:19 AM Gravatar
Nice tutorial, but I'm using Page and there are Page.Resources..So I can't use your solution. Any workarounds to this?

# re: WPF: Inheriting from custom class instead of Window

left by Jon at 12/19/2011 10:32 AM Gravatar
Very useful! Thank you :)

Depending on the level of experience it may be a little hard to understand (I had to read some of the comments in order to solve some doubts). Some code samples could make it easier indeed!

# re: WPF: Inheriting from custom class instead of Window

left by Bob Armour at 1/20/2012 11:11 AM Gravatar
Thanks for this article, Laurent - it has enabled me to avoid duplication of code over several very similar windows.

As mentioned by several people above, I would also be interested in any techniques for using common markup - very similar to the way that master pages work in ASP.NET MVC.

# re: WPF: Inheriting from custom class instead of Window

left by George Birbilis at 6/28/2012 4:49 PM Gravatar
also keep this in mind: http://support.microsoft.com/kb/957231

# re: WPF: Inheriting from custom class instead of Window

left by George Birbilis at 6/28/2012 5:18 PM Gravatar
regarding the MS link I posted above, http://msdn.microsoft.com/en-us/library/system.windows.markup.iaddchild.aspx says: "For purposes of establishing or defining a content property or content model in WPF, IAddChild is obsolete. Apply the ContentPropertyAttribute to a custom class instead."

So I think one could use a property on the ancestor to get the conent and mark it ALSO on the descendent using ContentPropertyAttribute or something if such class markup is not inherited

# re: WPF: Inheriting from custom class instead of Window

left by George Birbilis at 6/28/2012 5:47 PM Gravatar
A solution (complementing this article) that I found to work ok for visual inheritance of XAML markup (tried with Silverlight 5) while also supporting Visual Studio 2010 designer drag-drop is at: http://social.msdn.microsoft.com/forums/en/wpf/thread/4f0cd7e3-65fa-4d2a-b29f-492e4209641c
Comments have been closed on this topic.