cannot set multiple base to custom control error

essamce 621 Reputation points
2020-06-13T11:36:08.453+00:00

i'm trying to build a wpf custom control library, want to create some controls that inherited from wpf controls (textBox, label, ...) and i wanted them all to be inherted from a base class and wpf control, here is one of them:

public class MovableTextbox : TextBox, Movable


public class Movable : Control

and it always shows error :
Severity Code Description Project File Line Suppression State
Error CS1721 Class 'MovableTextbox' cannot have multiple base classes: 'TextBox' and 'Movable'

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,667 questions
{count} votes

Accepted answer
  1. DaisyTian-1203 11,616 Reputation points
    2020-06-15T02:08:02.97+00:00

    There is a workaround for you to add some custom Dependency Properties for your MovableTextbox which inherited from Textbox. You can create a class which inherit from DependencyObject to declare your custom Dependency Properties and use it like local:className.propertyName in the customControl. I will give you a demo to use it.
    Here is the class:

    public class RotationManager : DependencyObject
        {
            public static double GetAngle(DependencyObject obj)
            {
                return (double)obj.GetValue(AngleProperty);
            }
    
            public static void SetAngle(DependencyObject obj, double value)
            {
                obj.SetValue(AngleProperty, value);
            }
    
            public static readonly DependencyProperty AngleProperty =
                DependencyProperty.RegisterAttached("Angle", typeof(double), typeof(RotationManager), new PropertyMetadata(0.0, OnAngleChanged));
    
            private static void OnAngleChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
            {
                var element = obj as UIElement;
                if (element != null)
                {
                    element.RenderTransformOrigin = new Point(0.5, 0.5);
                    element.RenderTransform = new RotateTransform((double)e.NewValue);
                }
            }
        }
    

    The custom control code is:

     public class MyTextBox:TextBox
        {
            private bool m_underLine;
            public bool UnderLine
            {
                get { return m_underLine; }
                set
                {
                    if (this.m_underLine != value)
                    {
                        if (value)
                        {
                            this.TextDecorations = System.Windows.TextDecorations.Underline;
                        }
                        m_underLine = value;
                    }
                }
            }     
        }
    

    You can use it in the custom control like:

    <local:MyTextBox UnderLine="True" local:RotationManager.Angle="60" 
                             Width="400" Height="80"
                             Text="This is my custome TextBox"
                             ></local:MyTextBox>
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful