【问题标题】:How to Change Titlebar Buttons WPF C# [closed]如何更改标题栏按钮 WPF C# [关闭]
【发布时间】:2018-09-19 06:49:14
【问题描述】:

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

【问题讨论】:

  • 改变它的外观,或者按下它时的作用?

标签: c# wpf xaml titlebar


【解决方案1】:

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

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

一些有用的教程或起点可能是this MSDN articlehow 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>

【讨论】:

  • 我有问题。我无法移动窗口:/
【解决方案2】:

您可以使用 windowchrome 自定义窗口的镶边。 https://msdn.microsoft.com/en-us/library/system.windows.shell.windowchrome(v=vs.110).aspx

您可以使用它来完全重新模板化您的窗口。 请注意,自从编写以下内容以来,该类已移动。 https://blogs.msdn.microsoft.com/wpfsdk/2010/08/25/experiments-with-windowchrome/

这可能会产生奇怪的副作用,例如当您在 win10 中最大化窗口时。

【讨论】:

    【解决方案3】:

    您可以使用WindowChrome自定义窗口样式:https://github.com/DinoChan/WindowDemo

    此样式不使用 SystemParameters2 类,因为它在 .net 4.5 中不存在。

    【讨论】:

      猜你喜欢
      • 2013-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-15
      • 1970-01-01
      • 2010-10-25
      • 2019-01-15
      • 1970-01-01
      相关资源
      最近更新 更多