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.
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
source to share
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}"...
source to share