MSDN > Home page del forum > Windows Presentation Foundation (WPF) > Inheriting from inherited user control
Formula una domandaFormula una domanda
 

Con rispostaInheriting from inherited user control

  • giovedì 6 luglio 2006 4.22leovernazza Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    I'm having a problem with a user control generated code, when it inherits from other than UserControl.

    Steps:
    1. Add new User control
    2. Replace in the inheritance declaration UserControl for MyBaseUserControl
    3. Compile

    The generated class part is still inheriting from UserControl.
    You can change the ".g.cs" file, and sometimes you can get it succesfully compiled, but the error cames back again and again :)

    There is another way to do that?
    Thanks in advance...

Risposte

Tutte le risposte

  • giovedì 6 luglio 2006 12.36Marc Laroche Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con risposta
    Quick question:

    Have you changed Root XAML node of the MyBaseUserControl.xaml file to MyBaseUserControl as well?

    Let me know!!!


  • giovedì 6 luglio 2006 22.11leovernazza Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    Yes, it was the first problem. :S

    But then I get: "Error 7 'MyBaseUserControl' cannot be the root of a XAML file because it was defined using XAML."

    I can get it working if I convert it to a CustomControl (no xaml definition), but I loose the declarative inheritance.... mmm... I suppose I still can use templates and styles to reach that.

    Is this a XAML "design decision"?
  • martedì 2 ottobre 2007 17.44Ed Noepel Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    This seems to be a very important question for which I cannot find an answer.  Can a component defined in XAML be subclassed? This blog post asserts it can be done, but I have not found a way to get it to work.  Like everyone else, I get the following compile-time error when I try to subclass a Window I defined in XAML.

    error MC6017: 'MyCustomWindow' cannot be the root of a XAML file because it was defined using XAML.

    If we cannot subclass Windows defined in XAML, what are the best practices for coupling a base window style with common window functionality throughout an application?
  • martedì 2 ottobre 2007 21.31SerialSeb- Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     

    As the error message indicates, you can inherit from a class that inherits from Window, not from a Window you've created that has a .xaml and a .cs file. The former will be perfectly acceptable as a base class, the second will not.

     

    If you want to define a base window style, the way to go is to create a ControlTemplate for Window, and symply apply it through a style.

     

    Code Block

    <Window x:Class="CaffeineIT.Blog.WindowTemplate.Window1"

      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

      Title="Window1" Height="300" Width="300">

        <Window.Template>

            <ControlTemplate TargetType="{x:Type Window}">

                <Border Background="Beige" Padding="20" Margin="20">

                    <ContentPresenter />

                </Border>

            </ControlTemplate>

        </Window.Template>

        <TextBlock>Oh, I'm the content!</TextBlock>

    </Window>

     

     

  • giovedì 4 ottobre 2007 1.19Ed Noepel Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    Agreed.  The ctor of my BaseWindow currently sets its own Template property to a ControlTemplate (stored in a ResourceDictionary) defining the look-and-feel.  This is because I do not want to force all Windows deriving from BaseWindow to grab the ControlTemplate from the ResourceDictionary and set it explicitly.  It is important that classes deriving from my BaseWindow use the same ControlTemplate because some code in the BaseWindow class is needed to define the look (doing things not possible in XAML alone).

    The following post makes some good arguments about where inheritance should be used: markup subclassing. In my case, I am using inheritance for more than just polymorphism.  I am doing so because I must be fascist about the look of the derived Window.