Congratulations to all the winners of April 2013, they have won INR 3400 cash and INR 20147 worth prizes !
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 13287 |  Welcome, Guest!   Register  Login
Home > Articles > WPF > Define Base Class for Window / UserControl

Define Base Class for Window / UserControl

5 vote(s)
Rating: 2.6 out of 5
Article posted by Abhi2434 on 3/21/2010 | Views: 11871 | Category: WPF | Level: Beginner red flag


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

Download


 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.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About Abhishek Sur

Experience:3 year(s)
Home page:http://www.abhisheksur.com
Member since:Wednesday, December 02, 2009
Level:Silver
Status: [Member] [Microsoft_MVP] [MVP]
Biography:Working for last 2 and 1/2 years in .NET environment with profound knowledge on basics of most of the topics on it.
>> Write Response - Respond to this post and get points
Related Posts

This article explain ICommand and its member in detail in MVVM architecture.

Windows 7 came up with lots of goodies including better resource management, better performance, jumplist management, multitouch functionality & many more. Here I will discuss on developing a simple multitouch application using .Net 3.5 SP1.

The article will guide through the basics of WPF programing with in-depth knowledge about the architecture and the working principles of WPF programs. The article finally creates a sample "Hello World" application to step you into new foundation. Even though I have started from the beginning of WPF, I marked it as Intermediate article, as I will also go indepth of all the techniques while discussing it. This is the first part of the WPF turorial. Stay tune for others.

Easiest way to handle Globalization in your WPF application

WPF has an inbuilt feature of Spellchecking. In this article I have explored the SpellCheck functionality of WPF TextBoxBase object. CustomDictionaries introduced with WPF 4.0 is also discussed with this article

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/19/2013 8:40:27 PM