C # UserControl Inheritance

Working with VS.NET 2008, Output Type Class Library, Target Framework .NET 2.0

I came up with a simplified script to ask this question.

I have a custom item Button

, its a simple panel with one big button on it.

I want to create a control RedButton

that extends Button

, likewise GreenButton

.
eg.Class RedButton : Button

Ideally, when I open the constructor RedButton

, I will see the button that I created in Button

and can modify it, like making it red or changing the font, etc.

I tried to do this once, but when I open the constructor RedButton

, I just get a bunch of errors.

In this case, doing all this work programmatically is not an option for us, since in a real case it would be a pain.

Can anyone shed some light on this? Many thanks.

+1


source to share


6 answers


To be honest, your example should work fine. Just make sure you provide a default constructor for your derived class. Also make sure you are not using shared controls as the developer has no idea how to instantiate it.



+1


source


Is your build setup plugged in? Find the delay signature attribute as well as the checkbox in the project properties. I've seen the signing delay cause this kind of problem with VS2005, maybe it's still a problem in VS2008.



0


source


I had to deal with this problem for many years at an old company. Then I researched it a bit. I don't think there is a solution for this.

I don't know how much you want to extend the base class in your actual example, but the changes you mentioned in your example would be trivial. Just something like

btnTheButton.BackGround=Color.Red; 

      

In fact, perhaps any changes you need to make to the button can be done in minutes. Unfortunately it will take a few minutes every time you need to inherit a new control, but I think this is the only option

0


source


If the control you inherit from is from a DLL and not just another class in the solution, then your designer will render the inherited control correctly. I am guessing VS Design View needs a DLL to draw the control. There may be other ways as well.

0


source


Since VS.NET 2008 the root designer can present a "bunch of bugs" as you mentioned. In general, the scenario described should "just work".

What errors do you encounter?

0


source


Follow this example if you haven't already:

public class RedButton : Button

      

then in XAML instead of

<UserControl></UserControl>

      

Run / end the XAML file with

<Button></Button>

      

Note that it's okay to inherit from something in a different namespace, although you haven't defined a namespace yet. Example:

<radDock:RadPane ...            
        xmlns:radDock="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Docking"
        ...
        Title="{Binding Path=StudyTitle}"...

      

0


source







All Articles