如何在Wpf中将控件模板分配给窗口?

时间:2012-11-16 12:03:10

标签: c# wpf templates controltemplate

我正在尝试定义一个控件模板,然后我想用它来进行模态对话。问题是,我遵循了我可以在stackoverflow和其他任何地方找到的所有指令,但样式/模板没有加载和应用?相反,我得到一个静态资源异常。

那么,如果模板和窗口位于不同的文件中,如何将模板应用到我的窗口?

任何帮助?

<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}" />

我使用的模板就是这个:

<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>

1 个答案:

答案 0 :(得分:2)

使用DynamicResource而不是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}" />

将此添加到您的app.xaml

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