如何在WPF C#中更改标题栏按钮

3

我想在窗口标题栏上进行关闭、最小化和最大化按钮的自定义。一些程序中,标题栏是不同的,但我不想改变整个标题栏,只想改变按钮。有人可以帮忙吗?


1
改变它的外观或按下时的功能? - Xiaoy312
3个回答

4
一般来说,窗口的标题栏属于所谓的非客户区域。 这意味着您无法更改按钮外观,也无法添加自定义按钮等。
要实现此功能,您需要创建自己的窗口样式或使用可为您完成此操作的库。
一些有用的教程或起点可能是此MSDN文章如何创建自定义窗口上的教程。
要创建自己的窗口样式,您可以使用此简单样式作为基础:
<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>

我有一个问题。我无法移动窗口:/ - Burak Yeniçeri
MSDN的链接已经失效。 - Jack J

1

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接