我正在尝试定义一个控件模板,然后我想将其用于模式对话框。问题是,我遵循了在 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>