简体   繁体   English

如何更改 WPF Window 标题栏背景

[英]How to change WPF Window Title Bar Background

How to change the background color of wpf window title bar.如何更改 wpf window 标题栏的背景颜色。 I used the following code, but it didnt work.我使用了以下代码,但它没有工作。 I cannot see the close button when i change the color of the grid.当我更改网格的颜色时,我看不到关闭按钮。

Current当前的

原来的

Wanted通缉

通缉

 <Style x:Key="StandardStyle" TargetType="{x:Type Window}"> <Setter Property="WindowChrome.WindowChrome"> <Setter.Value> <WindowChrome /> </Setter.Value> </Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Window}"> <Grid Background="Yellow"> <Border Margin="50" > <ContentPresenter Content="{TemplateBinding Content}"/> </Border> <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Title}" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="36,8,0,0" Foreground="Red"/> <Image Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Icon}" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(WindowChrome.WindowChrome).ResizeBorderThickness}" Width="100" WindowChrome.IsHitTestVisibleInChrome="True"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>

When you extend your own content into the non-client area of a window using the WindowChrome class, you should define custom captions button yourself:当您使用WindowChrome class 将自己的内容扩展到 window 的非客户区时,您应该自己定义自定义字幕按钮

 <Style x:Key="StandardStyle" TargetType="{x:Type Window}"> <Setter Property="WindowChrome.WindowChrome"> <Setter.Value> <WindowChrome UseAeroCaptionButtons="False" /> </Setter.Value> </Setter> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Window}"> <Grid Background="Yellow"> <Border Margin="50" > <ContentPresenter Content="{TemplateBinding Content}"/> </Border> <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Title}" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="36,8,0,0" Foreground="Red"/> <Image Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Icon}" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(WindowChrome.WindowChrome).ResizeBorderThickness}" Width="100" WindowChrome.IsHitTestVisibleInChrome="True"/> <Button Content="&#xE106;" WindowChrome.IsHitTestVisibleInChrome="True" HorizontalAlignment="Right" VerticalAlignment="Top" Command="{x:Static ApplicationCommands.Close}"> <Button.Style> <Style TargetType="Button"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Grid x:Name="LayoutRoot" Background="Transparent" Width="44" Height="30"> <TextBlock x:Name="txt" Text="{TemplateBinding Content}" FontFamily="Segoe MDL2 Assets" FontSize="10" Foreground="#999999" HorizontalAlignment="Center" VerticalAlignment="Center" RenderOptions.ClearTypeHint="Auto" TextOptions.TextRenderingMode="Aliased" TextOptions.TextFormattingMode="Display"/> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="LayoutRoot" Property="Background" Value="#E5E5E5"/> <Setter TargetName="txt" Property="Foreground" Value="#000000"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Button.Style> </Button> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>

You also need to implement the command/event handler(s): https://stackoverflow.com/a/61784021/7252182您还需要实现命令/事件处理程序: https://stackoverflow.com/a/61784021/7252182

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM