Failed Scientist
Failed Scientist

Reputation: 2027

WPF UserControl inheriting another UserControl

I am trying to inherit an usercontrol in WPF the way mentioned in How can a WPF UserControl inherit a WPF UserControl?

namespace DMS.Presentation
{
    /// <summary>
    /// Interaction logic for WorkSpaceViewControl
    /// </summary>
    public abstract class WorkSpaceViewControl : UserControl
    {
        public WorkSpaceViewControl()
        {
            InitializeComponent();
        }

    private void InitializeComponent()
        {

        }
    }  

}

And code doesn't give any error thus far. But when I inherit it in a new usercontrol:

namespace DMS.Presentation
{
    /// <summary>
    /// Interaction logic for AnimalWorkSpaceView.xaml
    /// </summary>
    public partial class AnimalWorkSpaceView : WorkSpaceViewControl
    {
        public AnimalWorkSpaceView()
        {
            InitializeComponent();
        }

    }

}

And it's XAML file is:

//I have tried both WorkSpaceViewControl:UserControl and UserControl:WorkSpaceViewControl here


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

</UserControl:WorkSpaceViewControl>

I get a message that partial modifier doesn't exist. Another Partial Declaration of WorkSpaceViewControl exists. So how should I implement it and where have things gone wrong? My whole project is stuck due to this inheritance bottle-neck since January. Help will be really appreciated.

Upvotes: 3

Views: 3720

Answers (1)

Peter Duniho
Peter Duniho

Reputation: 70652

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.

Upvotes: 9

Related Questions