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()); } }
User controls, in WPF represented by the UserControl class, is the concept of grouping markup and code into a reusable container, so that the same interface, with the same functionality, can be used in several different places and even across several applications.
A window is managed by the OS and is placed on the desktop. A UserControl is managed by wpf and is placed in a Window or in another UserControl. Applcations could be created by have a single Window and displaying lots of UserControls in that Window.
This article explains the FrameworkElement class. This class is a base class that is inherited by all of the layout controls and other WPF Controls. FrameworkElement provides functionality related to the logical tree, styling, data binding and lifetime events.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With