Matthias
Matthias

Reputation: 5764

How to assign control template to window in Wpf?

I am trying to define a control template that I then want to use for example for modal dialogs. The problem is, that I followed all instructions that I could find on stackoverflow and anywhere else, but the style/template is not loaded and applied? Instead I get a static resource exception.

So, how do I apply the template to my window if the template and the window are in different files?

Any help?

<Window x:Class="WpfWindowTemplateTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        Template="{StaticResource MyWindowTemplate}"
        Style="{StaticResource MyWindowStyle}" />

The template that I was using is this one:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ControlTemplate x:Key="MyWindowTemplate" TargetType="{x:Type Window}">
        <Border x:Name="WindowBorder" Style="{DynamicResource WindowBorderStyle}">
            <Grid>
                <Border Margin="4,4,4,4" Padding="0,0,0,0" x:Name="MarginBorder">
                    <AdornerDecorator>
                        <ContentPresenter/>
                    </AdornerDecorator>
                </Border>
                <ResizeGrip Visibility="Collapsed" IsTabStop="false" HorizontalAlignment="Right" x:Name="WindowResizeGrip" 
                    VerticalAlignment="Bottom" />
            </Grid>
        </Border>
        <ControlTemplate.Triggers>
            <MultiTrigger>
                <MultiTrigger.Conditions>
                    <Condition Property="ResizeMode" Value="CanResizeWithGrip"/>
                    <Condition Property="WindowState" Value="Normal"/>
                </MultiTrigger.Conditions>
                <Setter Property="Visibility" TargetName="WindowResizeGrip" Value="Visible"/>
                <Setter Property="Margin" TargetName="MarginBorder" Value="4,4,4,20" />
            </MultiTrigger>
            <Trigger Property="WindowState" Value="Maximized">
                <Setter Property="CornerRadius" TargetName="WindowBorder" Value="0,0,0,0"/>
            </Trigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
    <Style x:Key="MyWindowStyle" TargetType="{x:Type Window}">
        <Setter Property="AllowsTransparency" Value="False" />
        <Setter Property="WindowStyle" Value="SingleBorderWindow" />
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
        <Setter Property="Background" Value="Transparent" />
        <Setter Property="ShowInTaskbar" Value="False" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Window}">
                    <Border>
                        <AdornerDecorator>
                            <ContentPresenter/>
                        </AdornerDecorator>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>    
</ResourceDictionary>

Upvotes: 1

Views: 7365

Answers (1)

Sisyphe
Sisyphe

Reputation: 4684

Use DynamicResource instead of StaticResource.

<Window x:Class="WpfWindowTemplateTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    Template="{DynamicResource MyWindowTemplate}"
    Style="{DynamicResource MyWindowStyle}" />

Add this to your app.xaml

<ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/WpfWindowTemplateTest;component/MyWindowTemplate.xaml" />
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>

Upvotes: 3

Related Questions