WindowChrome-无法单击标题栏中的按钮 [英] WindowChrome - Can't click buttons in titlebar

查看:101
本文介绍了WindowChrome-无法单击标题栏中的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的WPF应用程序使用了自定义WindowChrome样式(从此处复制: http://www.bdevuyst.com/wpf-custom-title-bar-and-taskbar/ )。

I have a custom WindowChrome style for my WPF app (ripped from here: http://www.bdevuyst.com/wpf-custom-title-bar-and-taskbar/).

此处的代码:

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity">

    <!-- Simple style without anything to remove the title bar -->
    <Style x:Key="Style.Window.WindowStyleNoneWithTaskbar.Base" TargetType="{x:Type Window}">
        <Setter Property="shell:WindowChrome.WindowChrome" >
            <Setter.Value>
                <shell:WindowChrome GlassFrameThickness="0" />
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Window}">
                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding Foreground}" BorderThickness="1" >
                        <ContentPresenter Content="{TemplateBinding Content}" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    <!-- Simple style with system buttons upper right -->
    <Style TargetType="{x:Type Window}" BasedOn="{StaticResource Style.Window.WindowStyleNoneWithTaskbar.Base}" x:Key="Style.Window.WindowStyleNoneWithTaskbar">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Window}">
                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding Foreground}" BorderThickness="1">
                        <Grid>
                            <ContentPresenter
                                Content="{TemplateBinding Content}" />

                            <Canvas Grid.Column="2" HorizontalAlignment="Right" Margin="0,6,10,0" VerticalAlignment="Top" >
                                <Button x:Name="MinimizeButton"  Style="{DynamicResource Style.Button.Core.Transparent}" Command="{Binding Source={x:Static SystemCommands.MinimizeWindowCommand}}" Width="24" Height="24" Canvas.Right="55" WindowChrome.IsHitTestVisibleInChrome="True" ToolTip="Close" >
                                    <Rectangle Margin="2" Fill="{DynamicResource appbar.reduce}" Opacity=".5" Width="15" Height="15" />
                                </Button>
                                <Button x:Name="NormalizeButton" Style="{DynamicResource Style.Button.Core.Transparent}" Visibility="Collapsed" Command="{Binding Source={x:Static SystemCommands.RestoreWindowCommand}}" Width="24" Height="24" Canvas.Right="30" WindowChrome.IsHitTestVisibleInChrome="True" ToolTip="Restore" >
                                    <Rectangle Margin="2" Fill="{DynamicResource appbar.normal}" Opacity=".5" Width="15" Height="15" />
                                </Button>
                                <Button x:Name="MaximizeButton" Style="{DynamicResource Style.Button.Core.Transparent}" Command="{Binding Source={x:Static SystemCommands.MaximizeWindowCommand}}" Width="24" Height="24" Canvas.Right="30"  WindowChrome.IsHitTestVisibleInChrome="True" ToolTip="Maximize" >
                                    <Rectangle Margin="2" Fill="{DynamicResource appbar.maximize}" Opacity=".5" Width="15" Height="15" />
                                </Button>
                                <Button x:Name="CloseButton" Style="{DynamicResource Style.Button.Core.Transparent}" Command="{Binding Source={x:Static SystemCommands.CloseWindowCommand}}" Width="24" Height="24" Canvas.Right="5" WindowChrome.IsHitTestVisibleInChrome="True" ToolTip="Close" >
                                    <Rectangle Margin="2" Fill="{DynamicResource appbar.close}" Opacity=".5" Width="15" Height="15" />
                                </Button>
                            </Canvas>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="WindowState" Value="Maximized">
                            <Setter Property="Visibility" Value="Collapsed" TargetName="MaximizeButton" />
                            <Setter Property="Visibility" Value="Visible" TargetName="NormalizeButton" />
                        </Trigger>

                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

我删除了对 shell:的引用,因为它已被弃用,但由于某些原因,我无法单击标题栏中的按钮。我从链接的网站下载了示例解决方案,并且按钮对我有效。

I removed references to "shell:" because it's deprecated, but for some reason I am unable to click the buttons in the title bar. I downloaded the sample solution from the website I linked, and the buttons worked from me.

因此,接下来,我将引用添加到 shell中,但是我仍然无法单击按钮。

So next, I added the references to "shell" back, but I was still unable to click the buttons.

我不确定我在做什么错。我几乎完全将示例解决方案复制到了我的项目中,并且 still 无效。我已将每个按钮的IsHitTestVisibleInChrome设置为 true,但没有区别。

I'm not sure what I'm doing wrong here. I pretty much completely copied the sample solution into my project and it still didn't work. I have IsHitTestVisibleInChrome set to "true" for each button but it's not making a difference.

我尝试了其他几种更简单的方法,使用了基本按钮,没有样式,都无济于事。我感觉好像这里缺少明显的东西。

I've tried several other, much more simple approaches, with basic buttons and no styling, all to no avail. I feel like I'm missing something obvious here.

推荐答案

这是您的命令绑定所在。不要绑定Command属性。只需设置它们:

It is your command bindings that are the issue here. Don't bind the Command properties. Simply set them:

<Canvas Grid.Column="2" HorizontalAlignment="Right" Margin="0,6,10,0" VerticalAlignment="Top" >
    <Button x:Name="MinimizeButton"  Style="{DynamicResource Style.Button.Core.Transparent}" Command="{x:Static SystemCommands.MinimizeWindowCommand}" Width="24" Height="24" Canvas.Right="55" WindowChrome.IsHitTestVisibleInChrome="True" ToolTip="Close" >
        <Rectangle Margin="2" Fill="{DynamicResource appbar.reduce}" Opacity=".5" Width="15" Height="15" />
    </Button>
    <Button x:Name="NormalizeButton" Style="{DynamicResource Style.Button.Core.Transparent}" Visibility="Collapsed" Command="{x:Static SystemCommands.RestoreWindowCommand}" Width="24" Height="24" Canvas.Right="30" WindowChrome.IsHitTestVisibleInChrome="True" ToolTip="Restore" >
        <Rectangle Margin="2" Fill="{DynamicResource appbar.normal}" Opacity=".5" Width="15" Height="15" />
    </Button>
    <Button x:Name="MaximizeButton" Style="{DynamicResource Style.Button.Core.Transparent}" Command="{x:Static SystemCommands.MaximizeWindowCommand}" Width="24" Height="24" Canvas.Right="30"  WindowChrome.IsHitTestVisibleInChrome="True" ToolTip="Maximize" >
        <Rectangle Margin="2" Fill="{DynamicResource appbar.maximize}" Opacity=".5" Width="15" Height="15" />
    </Button>
    <Button x:Name="CloseButton" Style="{DynamicResource Style.Button.Core.Transparent}" Command="{x:Static SystemCommands.CloseWindowCommand}" Width="24" Height="24" Canvas.Right="5" WindowChrome.IsHitTestVisibleInChrome="True" ToolTip="Close" >
        <Rectangle Margin="2" Fill="{DynamicResource appbar.close}" Opacity=".5" Width="15" Height="15" />
    </Button>
</Canvas>

您还需要为每个命令绑定一个命令,并按照@Louis的建议以编程方式执行它们科特曼在这里:

You will also need a command binding for each of the commands and execute them programmatically as suggested by @Louis Kottmann here:

在WPF中,我可以有一个无边框的窗口,该窗口具有规则的最小化,最大化和关闭按钮吗?

这篇关于WindowChrome-无法单击标题栏中的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆