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時,標題欄的顏色也會跟着改變。