none
Define controls in base class, add controls in derived class RRS feed

  • Question

  • I have a class that is derived from UserControl.  I am adding some controls to it in via the code file.  There is no XAML file.  I have another class that then derives from this base class.  This class does have a XAML file associated with it.  If I don't add anything to this XAML file the controls from the base class are displayed.  As soon as I add to the XAML file though, these controls overwrite the controls originally defined in the base class.  Is there a way to have a set of common controls defined in the base class and then add to those controls using XAML in the derived classes?
    Monday, May 7, 2007 7:32 PM

Answers

  • "Adding" to the controls already created by the base class is not well-defined. All elements form a tree. Where in the tree would you expect the additional elements to be attached? In any particular case, you can probably identify a panel where you want the additional elements appended. But this is hard to generalize.

    You may consider doing things the other way around: Let the XAML-generated code form the derived class create its element tree. In that tree you can require some element (panel) to have a predefined name, like "baseControlsPlaceholder". Then, from InitializeComponent() of the derived class call some (protected) method on the base class and give it a reference to the placeholder panel where it should attach its controls.

    Certainly, other arrangements are possible. Essentially, you need to define your application-specific tree merging protocol.

    Tuesday, May 8, 2007 6:29 PM

All replies

  • if the controls in derived class added as a group not be in between the controls in base class, then may be create 2 usercontrols indepent of each other and place those 2 usercontrols on the page
    Monday, May 7, 2007 7:48 PM
    Moderator
  • I would do that if there was no way to do what I'm looking for.  But the controls have to talk to each other and it would be much nicer to subclass rather than forcing each to hold a reference to the other.
    Monday, May 7, 2007 8:08 PM
  • "Adding" to the controls already created by the base class is not well-defined. All elements form a tree. Where in the tree would you expect the additional elements to be attached? In any particular case, you can probably identify a panel where you want the additional elements appended. But this is hard to generalize.

    You may consider doing things the other way around: Let the XAML-generated code form the derived class create its element tree. In that tree you can require some element (panel) to have a predefined name, like "baseControlsPlaceholder". Then, from InitializeComponent() of the derived class call some (protected) method on the base class and give it a reference to the placeholder panel where it should attach its controls.

    Certainly, other arrangements are possible. Essentially, you need to define your application-specific tree merging protocol.

    Tuesday, May 8, 2007 6:29 PM
  • I was thinking along the same lines, like masterpages in asp.net, where we have placeholder where the derived class controls would go
    Tuesday, May 8, 2007 6:34 PM
    Moderator
  • Thanks, that helps.  Could I maybe do it the other way around by defining the placeholder in the base class and then overriding Content (or something similar) in the derived class so that it puts its XAML-defined controls into the placeholder?
    Tuesday, May 8, 2007 7:20 PM