【问题标题】:WindowChrome - Can't click buttons in titlebarWindowChrome - 无法单击标题栏中的按钮
【发布时间】:2017-06-13 19:26:49
【问题描述】:

我的 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”,但这并没有什么不同。

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

【问题讨论】:

    标签: c# wpf xaml button titlebar


    【解决方案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?

    【讨论】:

    • 感谢您的回复。我今晚回家后试试这个
    • 修复了它。另外,我没有添加我应该添加的代码隐藏命令绑定。我习惯在 ViewModel 中这样做。谢谢!
    【解决方案2】:

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

    <Button shell:WindowChrome.IsHitTestVisibleInChrome="True"
    

    而且您的按钮不会对点击做出反应 - 只需 REMOVE "shell:"

    <Button WindowChrome.IsHitTestVisibleInChrome="True"
    

    这将使它们起作用

    【讨论】:

      猜你喜欢
      • 2020-01-22
      • 1970-01-01
      • 1970-01-01
      • 2022-12-11
      • 2015-04-06
      • 1970-01-01
      • 2019-07-02
      • 1970-01-01
      • 2019-07-02
      相关资源
      最近更新 更多