none
WPF Window template

    Question

  • Hi guys! I am trying to define a window template that can be used by windows in other assemblies. I define a window template and store it in resource dictionary in some assembly. After, I use this template in other assembly on window definition in XAML. It looks that the template is accepted and I can see updated window in VS-2010 designer but when I add a new control to this window, the control is disappears from window (but still exist in XAML code). I also tried to apply the same template explicitly and it works well.

    Xaml code of generic.xaml in project that contains template definition, the ThemeInfo attribute is set and BuildAction property for this file is Page.
    <ResourceDictionary
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:MyProg.Resources">
    
          <ControlTemplate x:Key="{ComponentResourceKey TypeInTargetAssembly= {x:Type local:DialogResources}, ResourceId=DialogTemplate}">  
          <Border Width="Auto" Height="Auto" Name="windowFrame"  
               BorderBrush="#395984"
               BorderThickness="1"
               CornerRadius="0,20,20,20" 
               Background="AliceBlue">
    
            <Grid>
              <Grid.RowDefinitions>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition></RowDefinition>
              </Grid.RowDefinitions>
    
              <TextBlock Margin="1" Padding="5" Text="Template Window" FontWeight="Bold"/>
    
              <Border Background="#B5CBEF" Grid.Row="1" CornerRadius="0,0,20,20" >
                <AdornerDecorator>
                  <ContentPresenter/>
                </AdornerDecorator>
              </Border>
            </Grid>
          </Border>
        </ControlTemplate>
    </ResourceDictionary>
    

    DialogRespources - is an empty class that defined in MyProg.Resources assembly.

    Now I use this template in other assembly like this:
    <Window x:Class="MyProg.Dialog.CustomDialog"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="CustomWindowTest" Height="300" Width="300"
      xmlns:res="clr-namespace:MyProg.Resources;assembly=Prog.Resources"
      Template="{StaticResource {ComponentResourceKey TypeInTargetAssembly={x:Type res:DialogResources}, ResourceId=DialogTemplate}}">
    
      <Button Content="Button" Height="23" Name="button1" Width="75" ></Button>
    </Window>
    

    In this window I add a button but I can't to see it. When I define the TemplateControl explicitly (without using resource) I can see it.

    The other problem is that I get a following designer exception when use TargetType="{x:Type Window}" for template in resource: "'Window' ControlTemplate TargetType does not match templated type 'WindowInstance'." I could not find anything regarding this exception in Google.

    Please, help me to understand what is wrong in my code?

    Thanks in advance
    Friday, July 09, 2010 5:01 PM

Answers

  • Hi Yura,

    I can see what is going on there. The default style of ContentPresenter bind its content via

    Content="{TemplateBinding Content}"
    

    So if you didn't add TargetType="x:Type Window" to your template, the default binding will not work.

    Since your template works fine when it is typed, you can add TargetType="x:Type Window" to the control template in the generic.xaml file. It probably will solve your problem. I didn't encounter any error like you mentioned. You could take another try in a clear solution.

    Alternatively, you can overide the default style setting using the XAML code in my last post. It works all the time for me

              <AdornerDecorator>
                <ContentPresenter Content="{TemplateBinding Window.Content}" ContentTemplate="{TemplateBinding Window.ContentTemplate}"/>
              </AdornerDecorator>
    

    Please run the application after you modifer xaml files. The designer might not be showing the real thing if it encountered some error. If it still doesn't work, would you please try put that in a new solution to see if there are other issues that hide the button.

    If your problem still exists, would you please send a sample solution that can reproduce your problem to Support-v-minz@Hotmail.com, or you can send me your Email address so I can give you my working sample.

    By the way, which version of VS and WPF are you currently using? Which OS are you using?

    Thank you and have a nice day!

    Best regards,

    Min


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Thursday, July 15, 2010 8:52 AM

All replies

  • Hi Yura,

    In order to show content in the window, you need to bind Content property of the ContentPresenter to the Content property of the Window.

    With this line modified your code will work fine.

    <ContentPresenter Content="{TemplateBinding Window.Content}" ContentTemplate="{TemplateBinding Window.ContentTemplate}"/>
    

    If you still have any doubts or concerns about this issue, please feel free to let me know.

    Best regards,

    Min


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Tuesday, July 13, 2010 8:58 AM
  • Hi and thanks for the answer,

    I put mentioned XAML code into the AdornerDecorator but nothing has changed. I can see the template window in designer but can't to see the added button. When I declare the template and the window that uses this template in the same XAML file I can see this button on the template window in VS designer. The only thing I change in this case - I add the TargetType="x:Type Window" for the template definition and use the  x:Key instead of ComponentResourceKey. So I don't sure that the problem is in content presenter properties. Moreover, what does the "'Window' ControlTemplate TargetType does not match templated type 'WindowInstance'." mean?

    Thanks

     

    Wednesday, July 14, 2010 2:44 PM
  • Hi Yura,

    I can see what is going on there. The default style of ContentPresenter bind its content via

    Content="{TemplateBinding Content}"
    

    So if you didn't add TargetType="x:Type Window" to your template, the default binding will not work.

    Since your template works fine when it is typed, you can add TargetType="x:Type Window" to the control template in the generic.xaml file. It probably will solve your problem. I didn't encounter any error like you mentioned. You could take another try in a clear solution.

    Alternatively, you can overide the default style setting using the XAML code in my last post. It works all the time for me

              <AdornerDecorator>
                <ContentPresenter Content="{TemplateBinding Window.Content}" ContentTemplate="{TemplateBinding Window.ContentTemplate}"/>
              </AdornerDecorator>
    

    Please run the application after you modifer xaml files. The designer might not be showing the real thing if it encountered some error. If it still doesn't work, would you please try put that in a new solution to see if there are other issues that hide the button.

    If your problem still exists, would you please send a sample solution that can reproduce your problem to Support-v-minz@Hotmail.com, or you can send me your Email address so I can give you my working sample.

    By the way, which version of VS and WPF are you currently using? Which OS are you using?

    Thank you and have a nice day!

    Best regards,

    Min


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    Thursday, July 15, 2010 8:52 AM
  • Hi Min,

    There is still some problem in design time. I sent you an e-mail...

    Thanks

    Wednesday, July 28, 2010 5:54 AM