c# - WPF如何获得最大化/最小化按钮图标 - 自定义标题栏

标签 c# wpf windows

我正在尝试为我的应用程序创建自定义标题栏。我不知道如何向按钮添加窗口最大化或最小化图标。我希望我的图标看起来像这样

Maximize/Minimize example

这是我的按钮 XAML

            <Button Command="{x:Static SystemCommands.MaximizeWindowCommand}" Content="+" Canvas.Left="1020" Height="30" Width="30">
            <Button.Style>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="Background" Value="Black"/>
                    <Setter Property="TextBlock.Foreground" Value="White"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type Button}">
                                <Border Background="{TemplateBinding Background}">
                                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="#403c47"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
        </Button>
        <Button Command="{x:Static SystemCommands.MinimizeWindowCommand}" Content="" Canvas.Left="990" Height="30" Width="30" >
            <Button.Style>
                <Style TargetType="{x:Type Button}">
                    <Setter Property="Background" Value="Black"/>
                    <Setter Property="TextBlock.Foreground" Value="White"/>
                    <Setter Property="FontSize" Value="24"/>
                    <Setter Property="TextBlock.LineHeight" Value="5"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type Button}">
                                <Border Background="{TemplateBinding Background}">
                                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" Value="#403c47"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
        </Button>

感谢任何帮助:)

最佳答案

这是我使用过的代码的摘录。 (在我的例子中是一个可重用的 Window 类扩展,太长而无法完整发布)

XAML 2 按钮最小化和最大化

<Grid Margin="1,0,1,0">
    <Button x:Name="Restore" Command="{Binding Source={x:Static SystemCommands.RestoreWindowCommand}}"  Visibility="Collapsed" Style="{StaticResource WindowButtonStyle}">
        <Grid Width="30" Height="25" UseLayoutRounding="True" RenderTransform="1,0,0,1,.5,.5">
            <Path Data="M2,0 L8,0 L8,6 M0,3 L6,3 M0,2 L6,2 L6,8 L0,8 Z" Width="8" Height="8" VerticalAlignment="Center" HorizontalAlignment="Center"
                Stroke="{Binding Foreground, RelativeSource={RelativeSource AncestorType={x:Type Button}, Mode=FindAncestor}}" StrokeThickness="1"  />
        </Grid>
    </Button>
    <Button x:Name="Maximize" Command="{Binding Source={x:Static SystemCommands.MaximizeWindowCommand}}"  Style="{StaticResource WindowButtonStyle}">
        <Grid Width="31" Height="25">
            <Path Data="M0,1 L9,1 L9,8 L0,8 Z" Width="9" Height="8" VerticalAlignment="Center" HorizontalAlignment="Center"
                Stroke="{Binding Foreground, RelativeSource={RelativeSource AncestorType={x:Type Button}, Mode=FindAncestor}}" StrokeThickness="2"  />
        </Grid>
    </Button>
</Grid>

当窗口最大化/最小化时触发隐藏最小化/最大化按钮

<ControlTemplate.Triggers>
    <Trigger Property="WindowState" Value="Maximized">
        <Setter TargetName="Maximize" Property="Visibility" Value="Collapsed" />
        <Setter TargetName="Restore" Property="Visibility" Value="Visible" />
    </Trigger>
    <Trigger Property="WindowState" Value="Normal">
        <Setter TargetName="Maximize" Property="Visibility" Value="Visible" />
        <Setter TargetName="Restore" Property="Visibility" Value="Collapsed" />
    </Trigger>
    .....
</ControlTemplate.Triggers>

enter image description here

希望能适应您的需求。

关于c# - WPF如何获得最大化/最小化按钮图标 - 自定义标题栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32022761/

相关文章:

wpf - 如何在 MVVM 模型中实现数据验证?

c# - JumpList.GetJumpList 不包含最近的项目

windows - 在 Windows 上使用 python3 忽略 PYTHONPATH

windows - 如何将目录更改为路径中的特定文件夹?

c# - 更改所有管理员壁纸

c# - 用于单元测试目的的重载构造函数

c# - 如果我设置属性,为什么这些值会发生变化,就像对象通过引用传递一样?

c# - DataGridView - 如何让复选框充当单选按钮?

c# - 带有响应 header 的单元测试 webapi Controller

.net - 仅检查指定的按键是否按下