Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WCF and WF
  4. Custom user control with chain inheritance

Custom user control with chain inheritance

Scheduled Pinned Locked Moved WCF and WF
helpdotnetwpfcomoop
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    koleraba
    wrote on last edited by
    #1

    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

    G 1 Reply Last reply
    0
    • K koleraba

      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

      G Offline
      G Offline
      Gideon Engelberth
      wrote on last edited by
      #2

      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.

      K 1 Reply Last reply
      0
      • G Gideon Engelberth

        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.

        K Offline
        K Offline
        koleraba
        wrote on last edited by
        #3

        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.

        G 1 Reply Last reply
        0
        • K koleraba

          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.

          G Offline
          G Offline
          Gideon Engelberth
          wrote on last edited by
          #4

          You have two ways to inherit from a generic class:

          public class Base(Of T)
          ' stuff here
          End class

          public 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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups