tags:

views:

3851

answers:

3

The following WPF UserControl called DataTypeWholeNumber which works.

Now I want to make a UserControl called DataTypeDateTime and DataTypeEmail, etc.

Many of the Dependency Properties will be shared by all these controls and therefore I want to put their common methods into a BaseDataType and have each of these UserControls inherit from this base type.

However, when I do that, I get the error: Partial Declaration may not have different base classes.

So how can I implement inheritance with UserControls so shared functionality is all in the base class?

using System.Windows;
using System.Windows.Controls;

namespace TestDependencyProperty827.DataTypes
{
    public partial class DataTypeWholeNumber : BaseDataType
    {
        public DataTypeWholeNumber()
        {
            InitializeComponent();
            DataContext = this;

            //defaults
            TheWidth = 200;
        }

        public string TheLabel
        {
            get
            {
                return (string)GetValue(TheLabelProperty);
            }
            set
            {
                SetValue(TheLabelProperty, value);
            }
        }

        public static readonly DependencyProperty TheLabelProperty =
            DependencyProperty.Register("TheLabel", typeof(string), typeof(BaseDataType),
            new FrameworkPropertyMetadata());


        public string TheContent
        {
            get
            {
                return (string)GetValue(TheContentProperty);
            }
            set
            {
                SetValue(TheContentProperty, value);
            }
        }

        public static readonly DependencyProperty TheContentProperty =
            DependencyProperty.Register("TheContent", typeof(string), typeof(BaseDataType),
            new FrameworkPropertyMetadata());


        public int TheWidth
        {
            get
            {
                return (int)GetValue(TheWidthProperty);
            }
            set
            {
                SetValue(TheWidthProperty, value);
            }
        }

        public static readonly DependencyProperty TheWidthProperty =
            DependencyProperty.Register("TheWidth", typeof(int), typeof(DataTypeWholeNumber),
            new FrameworkPropertyMetadata());



    }
}
+8  A: 

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 xmal (as shown above).
Martin Harris
when I make that change I get the error: "...DataTypes.BaseDataType cannot be the root of a XAML file because it was defined using XAML", how do you get out of this circular reference?
Edward Tanguay
If you can just make the base class a normal type that inherits from UserControl without defining any xmal (so not a partial class split over two files). The problem is that you can't inherit xmal, so either the base or child class need to not have a xmal file. Either that or make your classes custom controls instead of a user controls. That way the appearance is defined outside of the partial class but you'll lose the ease of use of a User Control.
Martin Harris
That's it: if you make BaseDataType a normal class that inherits UserControl, then it works. Excellent, thanks.
Edward Tanguay
@Martin you might want to update your answer to reflect your comment and it's the real answer.
Bryan Anderson
+2  A: 

You can take a look at this post, which shows how to inherit user controls defined in XAML:

Svetoslav Savov's WPF Blog - User Control Inheritance in WPF

A: 

Thanks, it helped me

Eric Ouellet