使用WindowChrome类设置标题栏背景

时间:2019-02-08 00:09:07

标签: c# wpf window-chrome

我只想设置WPF窗口的标题栏和边框,但title背景属性看起来并不暴露在类中。

我要保留所有默认的Window行为,只设置color propertyTitle Bar的{​​{1}}

要设置的正确属性是什么?

我正在指责:https://docs.microsoft.com/en-us/dotnet/api/system.windows.shell.windowchrome?view=netframework-4.7.2

Border

我看到的唯一属性是“标题属性”,设置其颜色无效

1 个答案:

答案 0 :(得分:1)

所以我解决了这个问题,它比我最初想到的要复杂得多。由于标题栏在编辑时位于非工作区中,因此我将失去角按钮的可见性。

最后,我不得不重新构建标题栏并创建一个类来实现按钮,以得到所需的外观。

这将为您提供带边框的窗口,标题栏也将具有颜色。不过,您需要实现角按钮。

   xmlns:shell="http://schemas.microsoft.com/netfx/2009/xaml/presentation"
   <Style x:Key="StandardStyle" TargetType="{x:Type Window}">
                <Setter Property="shell:WindowChrome.WindowChrome">
                    <Setter.Value>
                        <shell:WindowChrome 
                              />
                    </Setter.Value>
                </Setter>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type Window}" >
                            <Grid>
                                <!--Title Panel-->
                                <DockPanel LastChildFill="True">
                                    <Border Background="Blue" DockPanel.Dock="Top" 
                                    Height="{x:Static SystemParameters.CaptionHeight}" x:Name="titlebar">
                                        <Grid>
 <!--Title text only-->
                                            <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Title}" 
                                VerticalAlignment="Top" HorizontalAlignment="Center" Background="Transparent"  />
                                            <usercontrols:CornerButtons HorizontalAlignment="Right"/>
                                        </Grid>

                                    </Border>
                                    <!--Provides the actual content control-->
                                    <Border Margin="0,0,0,0"  >
                                        <AdornerDecorator>
                                            <ContentPresenter Content="{TemplateBinding Content}"/>
                                        </AdornerDecorator>
                                    </Border>
                                </DockPanel>

                                 <!--Provides the actual window border-->
                                <Border 
                                        Margin="0,0,0,0"
                                        Background="White"
                                        Grid.ZIndex="-1"
                                        BorderThickness="2,2,2,2" BorderBrush="Blue"
                                       >
                                </Border>                              

                                <!--This is the top left system button-->
                                <!--<Button
                                Margin="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(shell:WindowChrome.WindowChrome).ResizeBorderThickness}"
                                Padding="1"
                                HorizontalAlignment="Left"
                                VerticalAlignment="Top"
                                shell:WindowChrome.IsHitTestVisibleInChrome="True"
                                Command="{x:Static shell:SystemCommands.ShowSystemMenuCommand}"
                                CommandParameter="{Binding ElementName=CalcWindow}">
                                    <Image
                                    Width="16"
                                    Height="16"
                                    shell:WindowChrome.IsHitTestVisibleInChrome="True"
                                    Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Icon}" />
                                </Button>-->
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>