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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13416238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:14:47  来源:igfitidea点击:

How to assign control template to window in Wpf?

c#wpftemplatescontroltemplate

提问by Matthias

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.

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

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>

回答by Sisyphe

Use DynamicResource instead of StaticResource.

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

Add this to your app.xaml

将此添加到您的 app.xaml

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