I have a WPF Windows application. I need to change the background color of the title bar. How can I do that?
问题:
回答1:
In WPF the titlebar is part of the non-client area, which can't be modified through the WPF window class. You need to manipulate the Win32 handles (if I remember correctly).
This article could be helpful for you: Custom Window Chrome in WPF.
回答2:
Here's an example on how to achieve this:
<Grid DockPanel.Dock="Right"
HorizontalAlignment="Right">
<StackPanel Orientation="Horizontal"
HorizontalAlignment="Right"
VerticalAlignment="Center">
<Button x:Name="MinimizeButton"
KeyboardNavigation.IsTabStop="False"
Click="MinimizeWindow"
Style="{StaticResource MinimizeButton}"
Template="{StaticResource MinimizeButtonControlTemplate}" />
<Button x:Name="MaximizeButton"
KeyboardNavigation.IsTabStop="False"
Click="MaximizeClick"
Style="{DynamicResource MaximizeButton}"
Template="{DynamicResource MaximizeButtonControlTemplate}" />
<Button x:Name="CloseButton"
KeyboardNavigation.IsTabStop="False"
Command="{Binding ApplicationCommands.Close}"
Style="{DynamicResource CloseButton}"
Template="{DynamicResource CloseButtonControlTemplate}"/>
</StackPanel>
</Grid>
</DockPanel>
Handle Click Events in the code-behind.
For MouseDown -
App.Current.MainWindow.DragMove();
For Minimize Button -
App.Current.MainWindow.WindowState = WindowState.Minimized;
For DoubleClick and MaximizeClick
if (App.Current.MainWindow.WindowState == WindowState.Maximized)
{
App.Current.MainWindow.WindowState = WindowState.Normal;
}
else if (App.Current.MainWindow.WindowState == WindowState.Normal)
{
App.Current.MainWindow.WindowState = WindowState.Maximized;
}
I hope this helps.
回答3:
You can also create a borderless window, and make the borders and title bar yourself
回答4:
Check the following sample WPF customizing the appearance of a window in XAML
This sample shows how to fully customize the style/appearance of a window, including the non-client areas: title bar, borders, max, min and close buttons whilst still providing all the expected functionality.