c# - 如何更改标题栏按钮 WPF C#

标签 c# wpf xaml titlebar

<分区>

我想对窗口标题栏中的关闭、最小化和最大化按钮进行自定义。在某些程序中,标题栏不同,但我不想更改整个标题栏,只更改按钮。谁能帮我解决这个问题?

最佳答案

一般来说,窗口的标题栏属于窗口的所谓非客户区。 这意味着您无法更改按钮的外观,也无法添加自定义按钮等。

为此,您需要滚动自己的窗口样式或使用为您执行此操作的库。

一些有用的教程或起点可能是 this MSDN article或本教程 how to create a custom window .

要创建自己的窗口样式,可以使用这个简单的样式作为基础:

<Style x:Key="MyCustomWindowStyle" TargetType="{x:Type Window}"> 
    <Setter Property="WindowStyle" Value="None"/> 
    <Setter Property="AllowsTransparency" Value="True"/>
    <Setter Property="Background" Value="White"/> 
    <Setter Property="BorderBrush" Value="Blue"/> 
    <Setter Property="BorderThickness" Value="1"/>                        
    <Setter Property="Template">
        <Setter.Value>
           <ControlTemplate TargetType="{x:Type Window}">
                <Grid Background="{TemplateBinding Background}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>

                    <!-- this displays the window title -->
                    <TextBlock TextAlignment="Center"
                               Text="{TemplateBinding Title}"/>

                    <StackPanel Grid.Column="1"
                                Orientation="Horizontal"
                                HorizontalAlignment="Stretch"
                                VerticalAlignment="Center">

                        <!-- the minimize button, using your own style -->
                        <Button Style="{StaticResource MyMinimizeButtonStyle}" 
                                Width="20" 
                                Height="20" />

                        <!-- the close button, using your own style -->
                        <Button Style="{StaticResource MyCloseButtonStyle}"
                                Width="20"
                                Height="20" />
                    </StackPanel>

                    <!-- this displays the actual window content -->
                    <ContentPresenter Grid.Row="1"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

关于c# - 如何更改标题栏按钮 WPF C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49740264/

相关文章:

c# - 加密数据 (AES) 的大小错误

c# - 如何将 Windows 窗体保存为 pdf

c# - 绑定(bind)运行次数可变的 RichTextBox

c# - 如何直接绑定(bind)到 subview 模型?

c# - 创建 Xamarin.Forms 跨平台应用程序时出现 "This project requires a Visual Studio update to load"错误

c# - 属性 - 按值还是引用?

c# - 如何列出相机可用的视频分辨率

c# - 使用 MVVM Light 时处理复选框的最佳方法是什么

c# - 将数据绑定(bind)到 WPF 用户控件

c# - 是否可以在 wpf 弹出窗口中放置彩色背景?