How can a WPF UserControl inherit a WPF UserControl? How can a WPF UserControl inherit a WPF UserControl? wpf wpf

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).


public partial class MooringConfigurator : MooringLineConfigurator    {        public MooringConfigurator()        {            InitializeComponent();        }    }<dst:MooringLineConfigurator x:Class="Wave.Dashboards.Instruments.ConfiguratorViews.DST.MooringConfigurator"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    xmlns:dst="clr-namespace:Wave.Dashboards.Instruments.ConfiguratorViews.DST"    mc:Ignorable="d"    d:DesignHeight="300" d:DesignWidth="400">    <Grid x:Name="LayoutRoot" Background="White">    </Grid></dst:MooringLineConfigurator>    


I found the answer in this article: http://www.paulstovell.com/xmlnsdefinition

Basically what is says is that you should define an XML namespace in the AssemlyInfo.cs file, which can the be used in the XAML. It worked for me, however I placed the base user control class in a separate DLL...