How Can a Wpf Usercontrol Inherit a Wpf Usercontrol

How can a WPF UserControl inherit a WPF UserControl?

Ensure that you have changed the first tag in the xaml to also inherit from your new basetype

So

<UserControl x:Class="TestDependencyProperty827.DataTypes.DataTypeWholeNumber"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
>

becomes

<myTypes:BaseDataType x:Class="TestDependencyProperty827.DataTypes.DataTypeWholeNumber"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
xmlns:myTypes="clr-namespace:TestDependencyProperty827.DataTypes"
>

So, to summarise the complete answer including the extra details from the comments below:

  • The base class should not include a xaml file. Define it in a single (non-partial) cs file and define it to inherit directly from Usercontrol.
  • Ensure that the subclass inherits from the base class both in the cs code-behind file and in the first tag of the xaml (as shown above).

Inheriting from a UserControl in WPF

AFAIK you cannot inherit the xaml, you can only inherit the code behind.

We recently encountered the same problem on our project. The way we ended up solving our problem was to create a usercontrol and adding it to the "child" usercontrol.

If that doesnt work/help take a look at this:
https://web.archive.org/web/20200815091447/http://geekswithblogs.net/lbugnion/archive/2007/03/02/107747.aspx[1]

WPF UserControl inheriting another UserControl

According to the answer you've referenced, your derived UserControl XAML should look more like this:

<local:WorkSpaceViewControl x:Class="DMS.Presentation.AnimalWorkSpaceView"
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"
xmlns:local="clr-namespace:DMS.Presentation"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
</local:WorkSpaceViewControl>

You had declared two different XML namespaces, local and WorkSpaceViewControl, both referring to "clr-namespace:DMS.Presentation". You need just one of them (so I kept local, it being more idiomatic), and you need to use the namespace to qualify the type name WorkSpaceViewControl.

Thus, the XAML declaration starts as <local:WorkSpaceViewControl ...

In addition, the x:Class value for your derived class needs to be the derived class, not the base class. So instead of "DMS.Presentation.WorkSpaceViewControl", that should be set to "DMS.Presentation.AnimalWorkSpaceView" as shown above.

UserControl Inheritance WPF

It appears you inherited from ViewBase in code behind but UserControl in XAML.

LoginView.xaml

<Local.View:ViewBase x:Class="YourNameSpace.View.LoginView"
x:TypeArguments="Local.ViewModel:LoginViewModel"
xmlns:Local.View="clr-namespace:YourNameSpace.View"
xmlns:Local.ViewModel="clr-namespace:YourNameSpace.ViewModel">
</Local.View:ViewBase>

LoginView.xaml.cs

namespace YourNameSpace.View
{
public partial class LoginView : ViewBase<LoginViewModel>
{
public LoginView(LoginViewModel viewModel)
: base(viewModel)
{
InitializeComponent();
}
}
}

ViewBase.cs

namespace YourNameSpace.View
{
public abstract class ViewBase<T> : UserControl
where T : ViewModelBase
{
protected ViewModelBase ViewModel;

public ViewBase(T viewModel)
: base()
{
ViewModel = viewModel;
DataContext = ViewModel;
}
}
}

How do you inherit Dependent Properties from a base WPF User Control to a new User Control that is inherited?

You cannot reuse the content of the base UserControl as the content will be overridden by the derived controls. The base class should only define the dependency properties and don't have any XAML markup.

Please refer to the following sample code:

Test.vb (the base class):

Public MustInherit Class Test
Inherits UserControl

Public Shared ReadOnly TestTitleProperty As DependencyProperty = DependencyProperty.Register("TestTitle", GetType(String), GetType(Test), New UIPropertyMetadata(String.Empty, AddressOf TestChanged))

Public Property TestTitle As String
Get
Return CType(GetValue(TestTitleProperty), String)
End Get
Set
SetValue(TestTitleProperty, Value)
End Set
End Property

Private Shared Sub TestChanged(d As DependencyObject, e As DependencyPropertyChangedEventArgs)
'...
End Sub
Public MustOverride Sub DoThing()
End Class

UserControl1.xaml:

<local:Test x:Class="UserControl1"
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"
xmlns:local="clr-namespace:WpfApplicationVb1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>

</Grid>
</local:Test>

UserControl1.xaml.vb:

Public Class UserControl1
Inherits Test

Public Overrides Sub DoThing()
'...
End Sub
End Class

Trying to get a WPF UserControl to Inherit a Class

The base class in the XAML is still set to UserControl. Change it to Player. Also note that the namespace for the Player type will have to be defined. i.e:

<BaseClasses:Player x:Class="VisualA"
xmlns:BaseClasses="clr-namespace:MyProject.BaseClasses"
... all your other namespaces used

WPF - UserControl inheritance

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



Related Topics



Leave a reply



Submit