question

essamce avatar image
essamce asked ·

cannot set multiple base to custom control error

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-wpf
3 comments
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi,
in C# it's impossible to inherit from more then one class.

You can Moveable class inherit from TextBox and MoveableTextBox inherit from Moveable.

You can use partial key word to extent any class.

You can write Extensions to extent class with additional methods.

1 Vote 1 · ·
essamce avatar image essamce PeterFleischer-3316 ·

<p>hi <a rel="user" nodeId="1407290" href="/answers/users/1407290/peterfleischer-3316.html">@PeterFleischer-3316</a> , i need this base class to share some DependencyProperty ,
i can make this base class (Movable) inherits from Control , then write my moveable controls all inherited from this base,
but the problem is i dont know how to make my MovableTextBox (which inherited from Movable) support all textbox functionality
</p>

0 Votes 0 · ·
essamce avatar image essamce PeterFleischer-3316 ·

simply I want to share some custom DependencyPropertyies with my customControls, and one of my customControls will be inherited from Textbox,

0 Votes 0 · ·

1 Answer

DaisyTian-MSFT avatar image
DaisyTian-MSFT answered ·

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>
Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.