【发布时间】:2020-07-18 20:16:18
【问题描述】:
在我的WPF 应用程序中,我正在尝试创建一个Custom Chrome Window [参考:WindowChrome]。我关注了Restyle Your Window 文章。我在我的项目中创建了Resource Dictionary file 及其代码隐藏,如下所示。然后我在 app.xaml 文件中引用了资源字典(如下所示)。如您所见,resource dictionary 文件中Style 标记的x:key 值为CustomWindowStyle。 问题:如何为窗口分配样式CustomWindowStyle?
MyResourceDictionaryWindowStyle.xaml:
<ResourceDictionary x:Class="MyWPFProject.WindowStyle"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyWPFProject">
<Style x:Key="CustomWindowStyle" TargetType="{x:Type Window}">
<Setter Property="WindowChrome.WindowChrome">
<Setter.Value>
<WindowChrome CaptionHeight="30"
CornerRadius="4"
GlassFrameThickness="0"
NonClientFrameEdges="None"
ResizeBorderThickness="5"
UseAeroCaptionButtons="False" />
</Setter.Value>
</Setter>
<Setter Property="BorderBrush" Value="Black" />
<Setter Property="Background" Value="Gray" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<Grid>
<Border Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="5,30,5,5">
<AdornerDecorator>
<ContentPresenter />
</AdornerDecorator>
</Border>
<DockPanel Height="30"
VerticalAlignment="Top"
LastChildFill="False">
<TextBlock Margin="5,0,0,0"
VerticalAlignment="Center"
DockPanel.Dock="Left"
FontSize="16"
Foreground="White"
Text="{TemplateBinding Title}" />
<Button x:Name="btnClose"
Width="15"
Margin="5"
Click="CloseClick"
Content="X"
DockPanel.Dock="Right"
WindowChrome.IsHitTestVisibleInChrome="True" />
<Button x:Name="btnRestore"
Width="15"
Margin="5"
Click="MaximizeRestoreClick"
Content="#"
DockPanel.Dock="Right"
WindowChrome.IsHitTestVisibleInChrome="True" />
<Button x:Name="btnMinimize"
Width="15"
Margin="5"
VerticalContentAlignment="Bottom"
Click="MinimizeClick"
Content="_"
DockPanel.Dock="Right"
WindowChrome.IsHitTestVisibleInChrome="True" />
</DockPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
MyResourceDictionaryWindowStyle.xaml.cs:
using System;
.......
using System.Windows;
namespace MyWPFProject
{
public partial class WindowStyle : ResourceDictionary
{
public WindowStyle()
{
InitializeComponent();
}
private void CloseClick(object sender, RoutedEventArgs e)
{
var window = (Window)((FrameworkElement)sender).TemplatedParent;
window.Close();
}
private void MaximizeRestoreClick(object sender, RoutedEventArgs e)
{
var window = (Window)((FrameworkElement)sender).TemplatedParent;
if (window.WindowState == System.Windows.WindowState.Normal)
{
window.WindowState = System.Windows.WindowState.Maximized;
}
else
{
window.WindowState = System.Windows.WindowState.Normal;
}
}
private void MinimizeClick(object sender, RoutedEventArgs e)
{
var window = (Window)((FrameworkElement)sender).TemplatedParent;
window.WindowState = System.Windows.WindowState.Minimized;
}
}
}
MainWindow.xaml:
<Window x:Class="MyWPFProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Button x:Name="btnTest" Content="Test" Click="btnTest_Click"/>
......
......
</Grid>
</Window>
更新:
App.xaml:
根据用户@Simon Stanford的建议here:您所要做的就是在您的app.xaml 文件中引用资源字典,然后将样式分配给窗口CustomWindowStyle。所以我引用了资源字典如下。 问题现在是:如何为 Window 分配 Style CustomWindowStyle?
<Application x:Class="MyWOFProject.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:MyWOFProject"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary Source="MyResourceDictionaryWindowStyle.xaml"/>
</Application.Resources>
</Application>
【问题讨论】:
-
这能回答你的问题吗? How to create custom window chrome in wpf?
-
@TomJoney 您建议的链接中的用户
@Simon Stanford的response 几乎正在做我所做的事情。但该用户建议:您所要做的就是引用 app.xaml 文件中的资源字典and then assign the Window the Style CustomWindowStyle。他建议的最后一部分是我正在寻找的方法。有什么建议吗? -
不太确定你没有从那里得到什么,但似乎你对 ResourceDictionaries 的知识有一定的差距,所以我建议你阅读一下:docs.microsoft.com/en-us/windows/uwp/design/… 以获得有关方面的帮助此事及帮助参考。
-
@TomJoney 您的链接提供了对该主题的一般理解。但是,它的子链接Every FrameworkElement can have a ResourceDictionary 一起来自
@thatfuy的回复帮助我解决了这个问题。由于Window也是FrameworkElement,因此您将其属性设置为Style="{StaticResource CustomWindowStyle}",因为用户@thatguy在下面的MainWindow.xaml代码中显示。感谢您提供帮助。 -
我很高兴你已经弄明白了——起初,很难解释你需要什么帮助,所以我提供了一个链接,我认为这会让你更加理解。干杯。