Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF - UserControl inheritance

I have problem with control inheritance in WPF. I created a UserControl named BaseUserControl. I want for this control to be a base control for other WPF userControls. So I wrote another UserControl called FirstComponent. In next step I changed this code

FirstComponent : UserControl

to this

FirstComponent : BaseControl

However during compilation I get this error

Partial declarations of 'controlinheritance.componenets.FirstComponent' must not specify different base classes 

What should I do to enable FirstComponent to derive from BaseControl?

EDIT Thanks to abhishek answer I managed to inherit controls . Howerver I have another question. In base class I specified a property public Grid _MainGrid { get; set; }. Now I want in my derived class create an instance of this grid. So I used this code Howerver I get an error Property '_MainGrid' does not have a value. Line 8 Position 36.

like image 732
Berial Avatar asked Oct 20 '10 20:10

Berial


1 Answers

Did you see my complete article on it?

http://www.dotnetfunda.com/articles/article832-define-base-class-for-window--usercontrol-.aspx

I hope that would help you in this.

If you try to execute the project, 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.

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" 
                  xmlns:local="clr-namespace:BaseWindowSample" 
                  Title="Window1">
...
</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

like image 133
abhishek Avatar answered Nov 19 '22 14:11

abhishek