WPF程序通常情况下没办法修改窗体标题栏的样式,包括标题栏的背景颜色。

不过借助一个叫Fluent.Ribbon的第三方控件,貌似可以修改标题栏的背景颜色。

可以通过NuGet来安装这个控件:Install-Package Fluent.Ribbon

 

修改App.xaml代码:




<Application x:Class="WpfDemo.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfDemo"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary Source="pack://application:,,,/Fluent;Component/Themes/Generic.xaml" />
</Application.Resources>
</Application>


 

XAML代码:



<Fluent:RibbonWindow x:Class="WpfDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Fluent="urn:fluent-ribbon"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfDemo"
mc:Ignorable="d" Background="green" Title="MainWindows"
Height="350" Width="525">

<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<Grid Grid.Row="1">
<TextBlock>My first window containing a Ribbon and something else.</TextBlock>
</Grid>
</Grid>
</Fluent:RibbonWindow>


后台代码:




using System.Windows;

namespace WpfDemo
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Fluent.RibbonWindow
{
public MainWindow()
{
InitializeComponent();
}
}
}




其实就是把原来的窗体从Window类继承的,改成继承自Fluent.RibbonWindow,前后台都需要修改。

修改以后,设置窗体的Background时,标题栏的颜色也会跟着改变。