鼠标悬停在文本块上而不是整个按钮时触发了IsMouseOver for Button

时间:2017-08-16 09:04:17

标签: wpf xaml

我有以下布局:

<Button VerticalAlignment="Stretch" Style="{StaticResource MyStyle} >
    <TextBlock VerticalAlignment="Center">MyText</TextBlock>
</Button>

样式定义如下:

<Style x:Key="BackButtonStyle"
           TargetType="{x:Type Button}">
        <Setter Property="BorderBrush" Value="Transparent" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border x:Name="GridArea"
                            Padding="8,4"
                            Margin="0,0,0,0">
                        <ContentControl>
                            <ContentPresenter x:Name="ContentPresenterArea"/>
                        </ContentControl>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="GridArea" Property="Background" Value="{StaticResource Some_Brush}" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

问题是只有当鼠标位于文本块上而不是整个区域时,背景画笔才会更改。我试了几件但没有效果......

1 个答案:

答案 0 :(得分:2)

GridAreanull背景。具有null背景的元素不会对鼠标做出反应。设置Background="Transparent"或更好地使用TemplateBindings:

<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="Template">
<Setter.Value>
    <ControlTemplate TargetType="{x:Type Button}">
        <Border x:Name="GridArea"
                BorderBrush="{TemplateBinding BorderBrush}"
                Background="{TemplateBinding Background}"
                Padding="8,4"
                Margin="0,0,0,0">