WPF user controls
-
Hi! I need to implement a few UserControls in WPF. Since they all have some common functionality I want to create a base class from which they would all derive. The problem is that UserControl has to extend the UserControl class and there is no multiple inheritance in c#. Does anybody know a good way around this problem? Every advice will be appreciated! Uros Bregar
-
Hi! I need to implement a few UserControls in WPF. Since they all have some common functionality I want to create a base class from which they would all derive. The problem is that UserControl has to extend the UserControl class and there is no multiple inheritance in c#. Does anybody know a good way around this problem? Every advice will be appreciated! Uros Bregar
-
You just use an inheritance chain Create your base class inheriting from the UserControl class
MyBaseControl : UserControl
Then your control inherits from your base class, simple (If I understood the question correctly :)ActualControl : MyBaseControl
I guess i didn't explain my problem well enough. When you create a user control in visual studio, you actually get three files. .cs, .xaml and .g.cs(when using xaml and c#). When you change the base class of the partial class defined in .cs file, you have to do the same for the class defined in the .g.cs file, since partial classes have to have the same base class. The problem is that the .g.cs file is auto-generated by VS and gets changed every time the content of the xaml file is re-compiled. Uros Bregar
-
I guess i didn't explain my problem well enough. When you create a user control in visual studio, you actually get three files. .cs, .xaml and .g.cs(when using xaml and c#). When you change the base class of the partial class defined in .cs file, you have to do the same for the class defined in the .g.cs file, since partial classes have to have the same base class. The problem is that the .g.cs file is auto-generated by VS and gets changed every time the content of the xaml file is re-compiled. Uros Bregar
Is this what you're looking for?
namespace MyNamespace
{
public partial class MyUserControlBase : UserControl
{
public MyUserControlBase()
{
}
}public partial class MyUserControl : MyUserControlBase { public MyUserControl() { InitializeComponent(); } }
}
<local:MyUserControlBase x:Class="MyNamespace.MyUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyNamespace"
Height="300" Width="300">
<Grid>
</Grid>
</local:MyUserControlBase>Mark Salsbery Microsoft MVP - Visual C++ :java: