Custom user control with chain inheritance
-
Hi I am trying to create a class, that inherits from UserControl, but not directly - in other words, I have a class MyUserControlBase which inherits from UserControl and than I have a class MyUserControl which extends MyUserControlBase. Below is the sample code: <pre> //File MyUserControlBase.cs public class MyUserControlBase : UserControl { protected virtual void DoSomething() { } } //File MyUserControl.xaml.cs public class MyUserControl : MyUserControlBase { protected override void DoSomething() { } } //File MyUserControl.xaml <local:MyUserControlBase x:class="Test.MyUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Test" x:Name="MyName"> </local:MyUserControlBase> </pre> This far everything works ok. The problem is that I need my DoSomething() method to be abstract not virtual. When I change it to abstract I also have to change the class MyUserControlBase to abstract, and that is were the problems start. When I compile it I get an error in MyUserControl.xaml,saying: The type 'MyUserControlBase' cannot have a Name attribute. Value types and types without a default constructor can be used as items within a ResourceDictionary. Does anyone knows what is the problem and maybe has some workaround for this situation. Any idea will be appreciated! Thank you Uros
-
Hi I am trying to create a class, that inherits from UserControl, but not directly - in other words, I have a class MyUserControlBase which inherits from UserControl and than I have a class MyUserControl which extends MyUserControlBase. Below is the sample code: <pre> //File MyUserControlBase.cs public class MyUserControlBase : UserControl { protected virtual void DoSomething() { } } //File MyUserControl.xaml.cs public class MyUserControl : MyUserControlBase { protected override void DoSomething() { } } //File MyUserControl.xaml <local:MyUserControlBase x:class="Test.MyUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Test" x:Name="MyName"> </local:MyUserControlBase> </pre> This far everything works ok. The problem is that I need my DoSomething() method to be abstract not virtual. When I change it to abstract I also have to change the class MyUserControlBase to abstract, and that is were the problems start. When I compile it I get an error in MyUserControl.xaml,saying: The type 'MyUserControlBase' cannot have a Name attribute. Value types and types without a default constructor can be used as items within a ResourceDictionary. Does anyone knows what is the problem and maybe has some workaround for this situation. Any idea will be appreciated! Thank you Uros
I would just remove the name from the xaml. I personally have not come across a case where I need the x:Name on the root xaml element of a UserControl or Window. I imagine that the reason most people put one is to databind to it. In those cases, I would use a RelativeSource binding instead of trying to bind by name.
-
I would just remove the name from the xaml. I personally have not come across a case where I need the x:Name on the root xaml element of a UserControl or Window. I imagine that the reason most people put one is to databind to it. In those cases, I would use a RelativeSource binding instead of trying to bind by name.
Hi Thanks Gideon, that is actually what I needed - but I kind'a didn't remember to use relative source Binding. Maybe just one more thing. Is it possible to make the class MyUserControlBase(from my original message) generic. What I have in mind is something like the following
public abstract class MyUserControlBase : UserControl { public MyUserControlBase :base() { } protected abstract T DoSometnihg(); }
If i make it generic, I don't know how to make MyUserControl class inherit from it. Thank you. -
Hi Thanks Gideon, that is actually what I needed - but I kind'a didn't remember to use relative source Binding. Maybe just one more thing. Is it possible to make the class MyUserControlBase(from my original message) generic. What I have in mind is something like the following
public abstract class MyUserControlBase : UserControl { public MyUserControlBase :base() { } protected abstract T DoSometnihg(); }
If i make it generic, I don't know how to make MyUserControl class inherit from it. Thank you.You have two ways to inherit from a generic class:
public class Base(Of T)
' stuff here
End classpublic class Derived1(Of T)
inherits Base(Of T)end class
public class Derived2
inherits Base(Of String)end class
I'm not sure if XAML supports using generic controls directly. If not, you will have to use option 2 to make an actual control.