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

标签 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”,但这没有什么不同。

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

最佳答案

对正确答案的小补充。如果你有类似的东西:

<Button shell:WindowChrome.IsHitTestVisibleInChrome="True"

并且您的按钮不会对点击使用react - 只需删除“shell:”

<Button WindowChrome.IsHitTestVisibleInChrome="True"

这会让他们工作

关于c# - WindowChrome - 无法单击标题栏中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41905412/

相关文章:

wpf - 在 UserControl 中设置 DataContext 会影响父级中的绑定(bind)

c# - WPF 文本 block 不能多行

c# - 显示枚举描述而不是名称

c# - Azure 云文件 - "The specifed resource name contains invalid characters."

c# - 以接口(interface)作为返回类型的 Entity Framework 和建模集合

c# - 如何确定特定文件实际上是 MP3 文件?

c# - 如何使用 Regex 将十进制值中的句点转换为对 URL 更友好的新闻报道

c# - 向类型 'System.Windows.Controls.ItemCollection' 的集合添加值引发异常

c# - 有没有办法通过异步方法在 C# (WPF) 中使用 Google Calendar API?

c# - 使用 MVVM 模式的 UI 设计