<分区>
简介
我创造了一个美丽的 WindowChrome
应用于我的窗口的样式。当我添加 ContentControl
然而,按照我的风格,应用程序进入中断模式。
我拼凑了来自 this youtube video 的代码, this article , this SO question和 Microsoft's documentation我想出了以下代码。
注意:下面的代码都被认为是相关,因为应用程序不能与这些部分中的任何一个一起运行(是的,我知道它可以在没有代码隐藏的情况下运行,但是不得不从 Visual Studio 而不是关闭按钮停止应用程序很烦人——这也是我想要完成的)。实际上,我已经精简了下面的代码,以便更容易使用。
代码
窗口.xaml
<Style x:Key="TestWindow" TargetType="{x:Type Window}">
<Setter Property="Background" Value="#FF222222"/>
<Setter Property="BorderBrush" Value="WhiteSmoke"/>
<Setter Property="BorderThickness" Value="5,30,5,5"/>
<Setter Property="WindowChrome.WindowChrome">
<Setter.Value>
<WindowChrome CaptionHeight="20"
CornerRadius="0"
GlassFrameThickness="0,0,0,-1"
NonClientFrameEdges="None"
ResizeBorderThickness="5"
UseAeroCaptionButtons="True"/>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<Grid>
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<AdornerDecorator>
<ContentPresenter/>
</AdornerDecorator>
</Border>
<DockPanel LastChildFill="True" VerticalAlignment="Top" Height="30">
<StackPanel DockPanel.Dock="Right"
Orientation="Horizontal"
VerticalAlignment="Center">
<Button x:Name="Button_Close"
WindowChrome.IsHitTestVisibleInChrome="True"
Width="{Binding ActualHeight, RelativeSource={RelativeSource Self}}"
Click="CloseClick">
<ContentControl Template="{StaticResource Icon_Close}" Height="10"/>
</Button>
</StackPanel>
<StackPanel DockPanel.Dock="Left"
Orientation="Horizontal"
VerticalAlignment="Center">
<Image x:Name="PART_WindowCaptionIcon"
Width="16"
Height="16"
Margin="0,0,6,0"
Source="{TemplateBinding Icon}"/>
<TextBlock x:Name="PART_WindowCaptionText"
Margin="6,0,0,0"
Padding="0">
<Run BaselineAlignment="Center"
Text="{TemplateBinding Title}"
Foreground="Black"/>
</TextBlock>
</StackPanel>
</DockPanel>
</Grid>
<ControlTemplate.Triggers>
<Trigger SourceName="PART_WindowCaptionIcon" Property="Source" Value="{x:Null}">
<Setter TargetName="PART_WindowCaptionIcon" Property="Visibility" Value="Collapsed"/>
<Setter TargetName="PART_WindowCaptionText" Property="Margin" Value="5,0,0,0"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
图标.xaml
Window.xaml 为 ContentControl
引用此文件Template
通过 App.xaml
的属性值.
<ControlTemplate x:Key="Icon_Close">
<Viewbox>
<Polygon Points="357,35.7 321.3,0 178.5,142.8 35.7,0 0,35.7 142.8,178.5 0,321.3 35.7,357 178.5,214.2 321.3,357 357,321.3 214.2,178.5" Fill="Black"/>
</Viewbox>
</ControlTemplate>
窗口.xaml.cs
public partial class Window : ResourceDictionary
{
public Window()
{
InitializeComponent();
}
private void CloseClick(object sender, RoutedEventArgs e)
{
var window = (System.Windows.Window)((FrameworkElement)sender).TemplatedParent;
window.Close();
}
}
问题
当行<ContentControl Template="{StaticResource Icon_Close}" Height="10"/>
存在(第 38 行),收到以下消息。当同一行被删除/注释掉时,应用程序运行时不会进入中断模式。
查看“输出”窗口时,我收到以下消息:
An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.
问题
此代码直接放在 Window
的 XAML 代码中时有效, 但当我尝试将其放入模板时失败了。
我的问题是:
- 为什么我的应用程序在
ContentControl
时进入中断模式放在Window
中的模板? - 我该如何解决这个问题?
- 请注意,我必须使用我的
ControlTemplate
来自Icons.xaml
文件并且对此内容的调用必须保留在窗口的Style
中(而不是窗口的实际 xaml)。
- 请注意,我必须使用我的