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 '_Main开发者_如何学JAVAGrid' does not have a value. Line 8 Position 36.
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
Well the reason for the initial error was because the class was actually a partial class that was listing a particular base inheritance somewhere else in addition to the location where you changed your base class.
As for your property 'inheritance', I suggest trying
public Grid MainGrid
{
get
{
return base.MainGrid;
}
set
{
base.MainGrid = value;
}
}
However I should note that this will not give you a link to any existing instance(s) of your base class. If you want there to be a guaranteed link in your derived class to the lone instance of that Grid, then you will have to make the base class property a static. In which case, your code will look like this...
public Grid MainGrid
{
get
{
return BaseControl.MainGrid;
}
set
{
BaseControl.MainGrid = value;
}
}
When you specify a Different base class for a Usercontrol in the XAML.cs file
FirstComponent : BaseControl
You should also change this in the XAML
<Base:BaseControl x:Class="FirstComponent"
xmlns:Base="clr-namespace:MyApplication.Base"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
</Grid>
</Base:BaseControl>
精彩评论