WindowChrome - 无法单击标题栏中的按钮

时间:2017-01-28 01:11:07

标签: c# wpf xaml button titlebar

我的WPF应用程序有自定义的WindowChrome样式(从此处翻录: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:”的引用,因为它已被弃用,但由于某种原因,我无法单击标题栏中的按钮。我从我链接的网站下载了示例解决方案,这些按钮也是我的工作。

接下来,我添加了对“shell”的引用,但我仍然无法单击按钮。

我不确定我在这里做错了什么。我几乎完全将示例解决方案复制到我的项目中,仍然不起作用。对于每个按钮,我将IsHitTestVisibleInChrome设置为“true”,但它没有任何区别。

我尝试了其他几种更简单的方法,基本按钮和没有样式,都无济于事。我觉得我在这里遗漏了一些明显的东西。

2 个答案:

答案 0 :(得分:1)

这是你的命令绑定问题。不要绑定Command属性。只需设置它们:

<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 Kottmann的建议以编程方式执行它们:

In WPF, can I have a borderless window that has regular minimize, maximise and close buttons?

答案 1 :(得分:0)

除了正确答案外还很小。如果您有类似的东西:

<Button shell:WindowChrome.IsHitTestVisibleInChrome="True"

您的按钮对点击没有反应-只需删除“ shell:”

<Button WindowChrome.IsHitTestVisibleInChrome="True"

这将使它们正常工作