鼠标按钮仅在其底部触发按钮

时间:2015-03-20 12:07:13

标签: c# wpf xaml

我已经将所有窗口都设置为Style = None,并将此用户控件放在它的右上角。

<UserControl x:Class="SayRecep.Controls.WindowControls"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="27" d:DesignWidth="80">
<UserControl.Resources>
    <Style TargetType="Button" x:Key="WindowControlButton">
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="HorizontalContentAlignment" Value="Center" />
        <Setter Property="Foreground" Value="#BBBBBB" />
        <Setter Property="Margin" Value="2,0" />
        <Setter Property="Background" Value="Blue" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border Background="Blue" Width="20" Height="16">
                    <ContentPresenter />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Foreground" Value="White" />
            </Trigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>
<Grid>
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,9,6,0">
        <Button ToolTip="Minimize" Style="{StaticResource WindowControlButton}" Click="Button_Click">
            <Button.Content>
                <Grid Width="12" Height="8" VerticalAlignment="Bottom" >
                    <Path Data="M0,1 L4,1 Z" VerticalAlignment="Center" HorizontalAlignment="Center"
                                                          Stroke="White" StrokeThickness="2"  />
                </Grid>
            </Button.Content>
        </Button>
        <Grid  MouseDown="Grid_MouseDown">
            <Button x:Name="Restore" ToolTip="Restore" Click="Restore_Click" Style="{StaticResource WindowControlButton}" Visibility="Visible">
                <Button.Content>
                    <Grid UseLayoutRounding="True" RenderTransform="1,0,0,1,.5,.5">
                        <Path Margin="0,0,0,0" Data="M2,0 L8,0 L8,6 M0,3 L6,3 M0,2 L6,2 L6,8 L0,8 Z" VerticalAlignment="Center" HorizontalAlignment="Center"
                                                              Stroke="White" StrokeThickness="1"  />
                    </Grid>
                </Button.Content>
            </Button>
            <Button x:Name="Maximize" ToolTip="Maximize" Click="Maximize_Click" Visibility="Collapsed" Style="{StaticResource WindowControlButton}" >
                <Button.Content>
                    <Grid>
                        <Path Data="M0,1 L9,1 L9,8 L0,8 Z" VerticalAlignment="Center" HorizontalAlignment="Center"
                              Stroke="White" StrokeThickness="2"  />
                    </Grid>
                </Button.Content>
            </Button>
        </Grid>
        <Button ToolTip="Close" Style="{StaticResource WindowControlButton}" Click="Button_Click_1" >
            <Button.Content>
                <Grid Width="12" Height="12" >
                    <Path Data="M0,0 L14,14 M0,14 L14,0" Stretch="Fill"  VerticalAlignment="Center" HorizontalAlignment="Center"
                                                              Stroke="White" StrokeThickness="2.25"  />
                </Grid>
            </Button.Content>
        </Button>
    </StackPanel>
</Grid>

我正在使用它们进行Mimimize,恢复/最大化和关闭窗口。

问题是鼠标只在下半部分识别按钮。

不会提升Click,MouseOver或者甚至不会从上半部分显示工具提示。

在模板中,您可以看到蓝色边框,实际上之前没有。我一直在分析这个让它透明可能解决的问题。但没有。

1 个答案:

答案 0 :(得分:1)

此样式现在可以使用

 <Style TargetType="Button" x:Key="WindowControlButton">
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="HorizontalContentAlignment" Value="Center" />
        <Setter Property="Foreground" Value="#BBBBBB" />
        <Setter Property="Margin" Value="2,0" />
        <Setter Property="Background" Value="Blue" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border Background="{TemplateBinding Background}">
                        <ContentPresenter />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="Red"/>
            </Trigger>
        </Style.Triggers>
    </Style>