views:

5487

answers:

5

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

+4  A: 

Well .. you create your base control

public abstract class BaseUserControl : UserControl{...}

then in the XAML file :

<Controls:BaseUserControl x:Class="Termo.Win.Controls.ChildControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:Namespace.Of.Your.BaseControl">

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.

sirrocco
This solution worked for me.
GeekyMonkey
As far as i can remember, the designer complains when you try to inherit an abstract class in XAML. Otherwise this should work fine.
Entrodus
A: 

I must still be doing something wrong. Here is what I am doing exactly.

I have a user Control with the the following XAML:

<UserControl x:Class="Class1.Control1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Controls="clr-namespace:class1"
    Height="200" Width="175">
<Grid>
</Grid>

and the folling .cs code

public partial class Control1 : UserControl{...}

I want to ineherit from control1 in another project and add more functionality.

I tried using a class like so:

public class Class2 : Class1.Control1

This didnt work though because there was no XAML

I was unable to use a User Control because I could not inherit from anyclass except UserControl

Do I need to use a CustomControl? Does anyone see what I am doing wrong?

+1  A: 

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

BFreeman
A: 

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:

<UserControl ... >

 <!-- My wrapping XAML -->
  <Common:DialogControl>
    <Common:DialogControl.Heading>
      <!-- Slot for a string -->
    </Common:DialogControl.Heading>
    <Common:DialogControl.Control>
      <!-- Concrete dialog's content goes here -->
    </Common:DialogControl.Control>
    <Common:DialogControl.Buttons>
      <!-- Concrete dialog's buttons go here -->
    </Common:DialogControl.Buttons>
  </Common:DialogControl>
 <!-- /My wrapping XAML -->

</UserControl>

Together with some handling code in codebehind it would be a nice base component for dialog windows.

Tomáš Kafka
A: 

It didn't work because you cannot inherit an abstract class. (I mean you cannot derive a usercontrol from an abstract class)

Gil