问题
I have a custom WindowChrome style for my WPF app (ripped from here: http://www.bdevuyst.com/wpf-custom-title-bar-and-taskbar/).
Code here:
<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>
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.
So next, I added the references to "shell" back, but I was still unable to click the buttons.
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.
回答1:
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>
You will also need a command binding for each of the commands and execute them programmatically as suggested by @Louis Kottmann here:
In WPF, can I have a borderless window that has regular minimize, maximise and close buttons?
回答2:
Small addition to the correct answer. If you have something like:
<Button shell:WindowChrome.IsHitTestVisibleInChrome="True"
And your buttons do not react to clicks - just REMOVE "shell:"
<Button WindowChrome.IsHitTestVisibleInChrome="True"
This will make them work
来源:https://stackoverflow.com/questions/41905412/windowchrome-cant-click-buttons-in-titlebar