I am fairly new to WPF and I am having a problem with inheriting from a user control.
I created a User Control and now I need to inherit from that control and add some more functionality.
Has anyone does this sort of thing before? Any help would be greatly appreciated.
Thank you
AFAIK you cannot inherit the xaml, you can only inherit the code behind.
We recently encountered the same problem on our project. The way we ended up solving our problem was to create a usercontrol and adding it to the "child" usercontrol.
If that doesnt work/help take a look at this: http://geekswithblogs.net/lbugnion/archive/2007/03/02/107747.aspx1
You can accomplish this by using a
delegate
.Essentially, you need to create an interface (
YourInterface
) which wraps up the functionality you want, then make both the user control and your class implement that interface.Next, make sure the user control has a reference to an object of type
IYourInterface
, so that when your user control attempts to invoke a method of theInterface
, it calls your class' method.Because both the user control and class implement the same interface, they can be seen as the same kind of object - meaning you can put them both into a collection of objects of type
IYourInterface
. This should give you the behavior you want.In ASP.NET I use this technique often, by having my classes inherit from
abstract class
ancestors. I don't understand why WPF doesn't support this. :(I may have a bit of a solution: Composition instead of inheritance - I have come up with control, that has 'content slots' assignable from outside through databinding, look at my SO thread.
Example of use:
Together with some handling code in codebehind it would be a nice base component for dialog windows.
I think that you can do this but that you will have to redefine any functions and possibly some other stuff that you reference in the xaml in the child class.
IE if you have a button click event that you subscribe to in the base class xaml you will need override the button click in the child class and call the base class button click event.
Not one hundred percent sure of the the details since it's my coworkers code that I'm drawing from but thought this would give a start to anyone looking to implement this in the future.
Well .. you create your base control
then in the XAML file :
And that should work.
EDIT: Hmm.. this example is useful when you have a base control without XAML and then inherit from it. The other way around(from a base control with Xaml) - I'm not sure how you can go about it.
EDIT2: Apparently from this post + comments i take that what you want might not be possible.
You cannot inherit the xaml code it self. However creating an abstract class of the codebehind, will allow you to edit in code behind, from a derived class object.
Xaml Code: { Window1.xaml }
CodeBehind: { Window1.xaml.cs }
Derived Class : { DisabledButtonWindow.cs }
although you cannot inherit from the wpf source it self, you are able to use this "Window1" control as a template for all other derived controls.