Click here to Skip to main content
16,022,339 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In C# WPF (desktop application) I have a solution in wich there is a library project where I created some user controls.
Once compiled I see user controls on VS toolbar and i can use them into other projects without any issue by using drag and drop on other projects' windows.

Now I simply would like to extend one of these user control creating a new class that simply inherits from one of these base user control.
(No additional xaml required...I just want to add some methods and functions.)

If I do that thing in ne same project of the base user control I get no issues on derived control and it's all right.

But, in this case, I want (I need) to do this into an other project of the solution.
In this other project I already use the base control (as is) dragging from toolbar in a correct way, so I think (but for sure I'm wrong) that all references are ok.

Doing this I do not have compile issues but at runtime I get exception because the child class cannot find the xaml of the base control...
And at design time, if I drag this derived control, i gat an error:
XDG0003

(Sorry for bad english)

...Can anyone help?

What I have tried:

I searched microsoft docs, googled, even asked to some ai bot but all the possible solutions I find tells me something I already do...(nothing more as described in the question)

maybe i saerch the wrong way...

All the time I tested a new possible solution, for better security I compiled, closed all editing windows on VS, closed the entire solution and then reloaded the solution.
Posted

1 solution

There are two ways to do this: Inheritance and Extension Methods.

Inheritance is simple: add a new class and derive it from the original:
C#
namespace MyProject
   {
   internal class MyNewControl : OriginalControl
      {
      ... Add your new stuff here ...
      }
   }
All the appropriate methods, fields, properties, events, and so on are available as a part of your new class, and your class code can access all non-private objects and methods in the base class.

Extension methods are harder, and are mostly used when the original class is sealed (and all structs are sealed so cannot be inherited from). They must be declared in a static class, and all methods must be declared as static (thought they can be accessed as instance methods if this is used in teh declaration:
C#
namespace ExtensionMethods
    {
    public static class MyExtensions
        {
        public static int MyNewMethod(this OriginalClass cls)
            {
            return cls.PropertyOfSometype;
            }
        }
    }
There is more info here: Extension Methods - C# | Microsoft Learn[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900