1

我在 xaml 中的Style标记内使用EventSetter并处理 MouseEnter 事件。但问题是鼠标输入事件只可见一瞬间,然后就消失了……为什么会这样?为什么它没有持续存在?

这是我定义样式的 xaml 文件

<Style x:Key="MouseOverHighlightStyle">
    <EventSetter Event="Button.MouseEnter" Handler="element_MouseEnter" />
    <Setter Property="Button.Padding" Value="5"/>
</Style>

这是我处理事件的 .cs 文件

namespace testing2
{
   partial class  stt
    {
    private void element_MouseEnter(object sender, MouseEventArgs e)
    {

        ((Button)sender).Background =
        new SolidColorBrush(Colors.LightGoldenrodYellow);
    }

}
4

1 回答 1

2

您看到的行为是因为在按钮的默认模板中,有一个IsMouseOver属性触发器,其中显式颜色设置为 value#FFBEE6FD

具体来说,触发器是这样的:

<Trigger Property="UIElement.IsMouseOver" Value="True">
   <Setter Property="Panel.Background" TargetName="border">
      <Setter.Value>
         <SolidColorBrush>#FFBEE6FD</SolidColorBrush>
       </Setter.Value>
   </Setter>
   <Setter Property="Border.BorderBrush" TargetName="border">
     <Setter.Value>
        <SolidColorBrush>#FF3C7FB1</SolidColorBrush>
     </Setter.Value>
   </Setter>
</Trigger>

但不幸的是,如果您必须更改该触发器中的值,则必须覆盖按钮的整个 ControlTemplate。

对于您的情况,它将是这样的(带有颜色LightGoldenrodYellow):

<Button>
  <Button.Template>
    <ControlTemplate TargetType="ButtonBase">
       <Border BorderThickness="{TemplateBinding Border.BorderThickness}"
               BorderBrush="{TemplateBinding Border.BorderBrush}"
               Background="{TemplateBinding Panel.Background}"
               Name="border"
               SnapsToDevicePixels="True">
          <ContentPresenter RecognizesAccessKey="True"
                            Content="{TemplateBinding ContentControl.Content}"
                            ContentTemplate="{TemplateBinding 
                                              ContentControl.ContentTemplate}"
                            ContentStringFormat="{TemplateBinding 
                                          ContentControl.ContentStringFormat}"
                            Name="contentPresenter"
                            Margin="{TemplateBinding Control.Padding}"
                            HorizontalAlignment="{TemplateBinding 
                                          Control.HorizontalContentAlignment}"
                            VerticalAlignment="{TemplateBinding 
                                            Control.VerticalContentAlignment}"
                            SnapsToDevicePixels="{TemplateBinding
                                               UIElement.SnapsToDevicePixels}"
                            Focusable="False" />
       </Border>
       <ControlTemplate.Triggers>
         <Trigger Property="Button.IsDefaulted" Value="True">
           <Setter Property="Border.BorderBrush" TargetName="border">
             <Setter.Value>
              <DynamicResource ResourceKey="{x:Static SystemColors.HighlightBrushKey}"/>
             </Setter.Value>
           </Setter>
          </Trigger>
          <Trigger Property="UIElement.IsMouseOver" Value="True">
            <Setter Property="Panel.Background" TargetName="border">
              <Setter.Value>
                <SolidColorBrush Color="LightGoldenrodYellow"/>
              </Setter.Value>
            </Setter>
            <Setter Property="Border.BorderBrush" TargetName="border">
              <Setter.Value>
                <SolidColorBrush>#FF3C7FB1</SolidColorBrush>
              </Setter.Value>
            </Setter>
           </Trigger>
           <Trigger Property="ButtonBase.IsPressed" Value="True">
              <Setter Property="Panel.Background" TargetName="border">
               <Setter.Value>
                 <SolidColorBrush>#FFC4E5F6</SolidColorBrush>
               </Setter.Value>
              </Setter>
              <Setter Property="Border.BorderBrush" TargetName="border">
                <Setter.Value>
                  <SolidColorBrush>#FF2C628B</SolidColorBrush>
                </Setter.Value>
              </Setter>
            </Trigger>
            <Trigger Property="ToggleButton.IsChecked" Value="True">
              <Setter Property="Panel.Background" TargetName="border">
                <Setter.Value>
                  <SolidColorBrush>#FFBCDDEE</SolidColorBrush>
                </Setter.Value>
              </Setter>
              <Setter Property="Border.BorderBrush" TargetName="border">
               <Setter.Value>
                <SolidColorBrush>#FF245A83</SolidColorBrush>
               </Setter.Value>
              </Setter>
             </Trigger>
             <Trigger Property="UIElement.IsEnabled" Value="False">
               <Setter Property="Panel.Background" TargetName="border">
                <Setter.Value>
                  <SolidColorBrush>#FFF4F4F4</SolidColorBrush>
                </Setter.Value>
               </Setter>
               <Setter Property="Border.BorderBrush" TargetName="border">
                 <Setter.Value>
                  <SolidColorBrush>#FFADB2B5</SolidColorBrush>
                 </Setter.Value>
               </Setter>
              <Setter Property="TextElement.Foreground"
                      TargetName="contentPresenter">
                <Setter.Value>
                  <SolidColorBrush>#FF838383</SolidColorBrush>
                </Setter.Value>
               </Setter>
             </Trigger>
           </ControlTemplate.Triggers>
         </ControlTemplate>
    </Button.Template>
 </Button>
于 2014-02-10T20:25:40.393 回答