• Upgrade your Internet Experience
  • Sign in
  • Microsoft.com
  • United States (English)
    Brasil (Português)Česká republika (Čeština)Deutschland (Deutsch)España (Español)France (Français)Italia (Italiano)Россия (Русский)대한민국 (한국어)中华人民共和国 (中文)台灣 (中文)日本 (日本語)香港特别行政區 (中文)
 
 
.NET Framework Developer Center
 
 
Home
 
 
Library
 
 
Learn
 
 
Downloads
 
 
Support
 
 
Community
 
 
Forums
 
 
 
.NET Framework Developer Center > .NET Development Forums > Windows Presentation Foundation (WPF) > Inheriting from inherited user control
Ask a questionAsk a question
Search Forums:
  • Search Windows Presentation Foundation (WPF) Forum Search Windows Presentation Foundation (WPF) Forum
  • Search All .NET Development Forums Search All .NET Development Forums
  • Search All MSDN Forums Search All MSDN Forums
 

AnswerInheriting from inherited user control

  • Thursday, July 06, 2006 4:22 AMleovernazza Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    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...
    • ReplyReply
    • QuoteQuote
     

Answers

  • Thursday, July 06, 2006 12:36 PMMarc Laroche Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Vote As Helpful
    0
    Quick question:

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

    Let me know!!!


    • ReplyReply
    • QuoteQuote
     

All Replies

  • Thursday, July 06, 2006 10:11 PMleovernazza Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    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"?
    • ReplyReply
    • QuoteQuote
     
  • Tuesday, October 02, 2007 5:44 PMEd Noepel Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    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?
    • ReplyReply
    • QuoteQuote
     
  • Tuesday, October 02, 2007 9:31 PMSerialSeb- Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0

    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>

     

     

    • ReplyReply
    • QuoteQuote
     
  • Thursday, October 04, 2007 1:19 AMEd Noepel Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Vote As Helpful
    0
    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.

    • ReplyReply
    • QuoteQuote
     
Need Help with Forums? (FAQ)
 
© 2009 Microsoft Corporation. All rights reserved.
Terms of Use
|
Trademarks
|
Privacy Statement