2014-02-10 68 views
1

我使用EventSetter inside Style xaml中的標記和處理MouseEnter事件。 但問題是,鼠標進入事件只是可見一會兒,然後消失......爲什麼這樣?爲什麼它不是持續?處理Xaml中Style中的事件

這是我所定義樣式

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

我的XAML文件,這是我的.cs文件,其中IM處理事件

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

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

} 
+2

你是什麼意思_mouse進入事件只是一會兒可見,並得到disappeard_? 'MouseEnter'是一個事件,並沒有狀態。如果你想狀態有'UIElement.IsMouseOver' – dkozl

+0

是...當我使用button.IsMouseOver它也出現一秒鐘然後消失...我想改變按鈕的背景,直到鼠標結束它! – Ravi

+0

它不是'MouseEnter'或'IsMouseOver' _disappears_,但'Button'有它的默認模板,當鼠標結束時它將改變背景顏色。您可以創建自己的模板並指定鼠標移動完成後應該發生的情況 – dkozl

回答

2

行爲你看到的是因爲在按鈕的默認模板裏有我是IsMouseOver屬性中的觸發器,其中明確的顏色設置爲值#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> 

但不幸的是,如果你要改變在觸發你必須重寫按鈕的整個控件模板值。

對於您的情況下,它會是這樣的(顏色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>