WPF应用程序所有窗口中的自定义标题栏

xtupzzrd  于 2022-12-14  发布在  其他
关注(0)|答案(1)|浏览(327)

我有自定义的标题栏为所有窗口的应用程序。我想创建一个模板,我可以使用多次在不同的窗口或消息框,我在想什么是最好的做法来实现这一点。
目前我有一个标题栏,但它是硬编码在每个窗口。
这是其中一个窗口的示例

<Window x:Class="MyApp.Configuration.Windows.NotMainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        Title="Some text" 
        Height="113.868" Width="405.84" 
        Background="#FFE5E5E5"
        WindowStyle="None"
        >
    <WindowChrome.WindowChrome>
        <WindowChrome CaptionHeight="34" />
    </WindowChrome.WindowChrome>

    <Window.Resources>
        <ResourceDictionary Source="pack://application:,,,/somefolder.somefolder;component/Resources/TitleBarIconsStyle.xaml" />
    </Window.Resources>
    <Border x:Name="MainWindowBorder"  BorderThickness="0" >
        <Grid x:Name="parentContainer">

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

            <!--Window chrome-->
            <Grid Grid.Row="0" Height="30" Background="#585856">
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center">
                    <!--App icon-->
                    <Image Source="/Resources/icon.png" Width="18" Margin="2" HorizontalAlignment="Left" VerticalAlignment="Center" />
                    <TextBlock FontFamily="Arial" Margin="4 3 0 0" Text="Some text"/>
                </StackPanel>

                <!--Caption buttons-->
                <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" >
                    <Button Style="{StaticResource MinimizeButtonStyle}" WindowChrome.IsHitTestVisibleInChrome="True" ToolTip="Minimize"
                             Click="CommandBinding_Executed_Minimize"/>
                    <Button x:Name="RestoreButton" Visibility="Collapsed" Style="{StaticResource RestoreButtonStyle}" 
                            Click="CommandBinding_Executed_Restore" WindowChrome.IsHitTestVisibleInChrome="True" ToolTip="Restore"/>
                    <Button x:Name="MaximizeButton" Visibility="Visible" Style="{StaticResource MaximizeButtonStyle}" 
                            Click="CommandBinding_Executed_Maximize" WindowChrome.IsHitTestVisibleInChrome="True" ToolTip="Maximize" />
                    <Button x:Name="CloseButton" Style="{StaticResource CloseButtonStyle}" WindowChrome.IsHitTestVisibleInChrome="True" ToolTip="Close"
                            Click="CommandBinding_Executed_Close"/>
                </StackPanel>
            </Grid>
            <Grid Grid.Row="1">
                <TextBox x:Name="Input"/>
                <Button x:Name="OkButton" 
                Content="Ok"/>

            </Grid>
        </Grid>
    </Border>
</Window>
dohp0rv5

dohp0rv51#

基于此MSDN文档,您无法在WPF应用程序中自定义(如更改颜色和添加图标)窗口标题栏。
解决方案是创建自定义标题栏样式并在<Window/>标记中设置WindowStyle="None"
在我的WPF应用程序中有一个自定义标题栏:

<Grid FlowDirection="RightToLeft">
        <Grid.RowDefinitions>
            <RowDefinition Height="25*"/>
        </Grid.RowDefinitions>
        
        <StackPanel Margin="0 0 5 0" Orientation="Horizontal" Background="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" Grid.Row="0" Grid.Column="6" Grid.ColumnSpan="2" FlowDirection="LeftToRight">
            <Button x:Name="btnCloseApp" IsEnabled="True" Margin="0 0 4 0" ToolTip="Exit" Style="{StaticResource MaterialDesignFloatingActionMiniAccentButton}" Height="25" Width="25" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="DodgerBlue" Click="btnCloseApp_Click">
                <materialDesign:PackIcon Kind="Shutdown" Height="20" Width="20"/>
            </Button>
            <Button x:Name="btnMinimizeApp" IsEnabled="True" ToolTip="Minimize" Style="{StaticResource MaterialDesignFloatingActionMiniAccentButton}" Height="25" Width="25" Background="{x:Null}" BorderBrush="{x:Null}" Foreground="DodgerBlue" Click="btnMiniApp_Click">
                <materialDesign:PackIcon Kind="WindowMinimize" Height="20" Width="20"/>
            </Button>
        </StackPanel>
</Grid>

图中:

同样,不要忘记设置WindowStyle="None"

相关问题