Define Base Class for Window / UserControl

Abhi2434
Posted by in WPF category on for Beginner level | Views : 19235 red flag
Rating: 2.6 out of 5  
 5 vote(s)

Here I have showed a simple trick to define base class for WPF Window and UserControl.


 Download source code for Define Base Class for Window / UserControl

Introduction


While doing your program in WPF, you may have found problems creating a BaseWindow Class to define methods that you often need may be for every page you define. Coming from ASP.NET programming, I have found this problem greatly as it could easily be done with ASP.NET Page class. WPF throws a nasty error when we just modify the Base Class of a window in code behind to one which is created custom to us. Even I got the same few days back. Thank god I finally got it solved with some trick. In this article, I am going to share with you this little trick which might come very handy to you.




The Trick


Basically the window Root element defines the Baseclass of the class you define. This is very essential. So if you have already changed baseclass of your window you must have to change the Root element of the XAML.  Lets see the procedure with an example.





The Procedure

As I already told you, To make this happen I have defined one class, named as BaseWindow which inherits from Window. In this class, it is important to define the default constructor, as I am going to use this class as my Window. Visual Studio implicitly calls the default constructor to create an object to show the Designer. So the Class would look like :

 public class BaseWindow : Window
{
public BaseWindow(string username)
{
this.UserName = username;
}
public BaseWindow()
:this("Default User")
{

}
Now, lets create a new Window say of name Window1 (which comes as default) and change the BaseClass from Window to BaseWindow. So the code behind of the Window1 will look like :

public partial class Window1 : BaseWindow
{
public Window1()
:base("Default User")
{
InitializeComponent();
}
}

Now, if you try to execute the project now, it would definitely throw error to you. This is because, every WPF window is created from the baseWindow layout rather than the current Window layout. In other words, if you see the XAML, you will see the root tag is Window, which is a class just parent of the current window. (In case of ASP.NET every page inherits from its own code behind).

Thus to ensure everything works perfectly, we need to change the Root Element.

So it would look like :

<local:basewindow class="BaseWindowSample.Window1" 
name="winImp"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
x="http://schemas.microsoft.com/winfx/2006/xaml"
local="clr-namespace:BaseWindowSample"
title="Window1" height="300" width="300">
<grid>
</grid>
</local:basewindow>
If you see this minutely, you can see I have added one namespace to my project and named it as local. So BaseWindow should come from BaseWindow and thus it goes like local:BaseWindow.

Now you can see, the error goes out and you can work with your project as normally. Please try out the sample application to have more clear idea on what I want to tell you.

Conclusion


So you can see this could easily be achieved. Now you can define as many Baseclasses as you want, and associate with the Windows or UserControls. Make sure you define the Default constructor for each class you define to ensure that the window works perfectly.

Thanks for reading.

Page copy protected against web site content infringement by Copyscape

About the Author

Abhi2434
Full Name: Abhishek Sur
Member Level: Silver
Member Status: Member,Microsoft_MVP,MVP
Member Since: 12/2/2009 4:19:08 AM
Country: India
www.abhisheksur.com
http://www.abhisheksur.com
Working for last 2 and 1/2 years in .NET environment with profound knowledge on basics of most of the topics on it.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)