2016-04-08 98 views
6

這裏是我的代碼:在WPF中,爲什麼MouseLeave會觸發而不是MouseDown?

<StackPanel> 
    <StackPanel.Resources> 
     <Style x:Key="stlNavButtonBorder" TargetType="Border"> 
      <Setter Property="BorderBrush" Value="#570000FF" /> 
      <Setter Property="BorderThickness" Value="5" /> 
      <Setter Property="Height" Value="100" /> 
      <Setter Property="Width" Value="200" /> 
      <Setter Property="Margin" Value="10" /> 

      <Style.Triggers> 
       <EventTrigger RoutedEvent="MouseEnter"> 
        <BeginStoryboard> 
         <Storyboard> 
          <ColorAnimation 
           Storyboard.TargetProperty="BorderBrush.Color" 
           To="blue" 
           Duration="0:0:0.25"/> 
         </Storyboard> 
        </BeginStoryboard> 
       </EventTrigger> 
       <EventTrigger RoutedEvent="MouseLeave"> 
        <BeginStoryboard> 
         <Storyboard> 
          <ColorAnimation 
           Storyboard.TargetProperty="BorderBrush.Color" 
           To="#570000FF" 
           Duration="0:0:0.25" /> 
         </Storyboard> 
        </BeginStoryboard> 
       </EventTrigger> 
       <EventTrigger RoutedEvent="MouseDown"> 
        <BeginStoryboard> 
         <Storyboard> 
          <ColorAnimation 
           Storyboard.TargetProperty="BorderBrush.Color" 
           To="Black" 
           Duration="0:0:0.25" /> 
         </Storyboard> 
        </BeginStoryboard> 
       </EventTrigger> 
      </Style.Triggers> 
     </Style> 
     <Style x:Key="stlNavButtonRectangle" TargetType="Rectangle"> 
      <Setter Property="Fill" Value="#570000FF"/> 
     </Style> 

     <Style TargetType="Button"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="Button"> 
         <Border Style="{StaticResource stlNavButtonBorder}"> 
          <Grid> 
           <Rectangle Style="{StaticResource stlNavButtonRectangle}"/> 
           <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> 
          </Grid> 
         </Border> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
    </StackPanel.Resources> 

    <Button Content="Button 1" /> 
    <Button Content="Button 2"/> 
    <Button Content="Button 3" /> 
</StackPanel> 

它產生這些按鈕:

enter image description here

當鼠標進入按鈕,它按預期工作,和邊框顏色發生變化: Blue border added to the picture when the mouse enters

問題是,當鼠標停在按鈕上時,邊框不會從藍色變爲黑色,因爲我試圖在MouseDown事件觸發器,而是消失,這是事件觸發器的MouseLeave

如何解決?謝謝。

+0

請參閱本 - https://social.msdn.microsoft.com/Forums/vstudio/en-US/1069c828-bf5b-4777-a3ab-30e39369a83f/mousedown-event-also-triggers-mouseleave-and -mouseenter-events?forum = wpf – PaulF

+0

如果您檢查默認控件模板,您會看到一些使用可視狀態組(過渡動畫),而另一些使用屬性上的觸發器,如「IsMouseOver」,「IsEnabled」和「IsPressed '。採用這些方法之一,您可能會獲得更多成功。 –

+0

@PaulF - 我無法弄清楚這個鏈接的解決方案... – Sipo

回答

1

我能不能找到潛在的問題。如果我沒有錯,MouseDown事件被Button吞噬Click事件。無論如何,我希望下面的代碼能幫助你解決這個問題。

更新:

這裏是更新的代碼,將保持IsPressed被觸發之後也MouseLeave觸發。

<StackPanel> 
    <StackPanel.Resources> 
     <Style x:Key="stlNavButtonBorder" TargetType="Border"> 
      <Setter Property="BorderBrush" Value="#570000FF" /> 
      <Setter Property="BorderThickness" Value="5" /> 
      <Setter Property="Height" Value="100" /> 
      <Setter Property="Width" Value="200" /> 
      <Setter Property="Margin" Value="10" /> 

     </Style> 
     <Style x:Key="stlNavButtonRectangle" TargetType="Rectangle"> 
      <Setter Property="Fill" Value="#570000FF"/> 
     </Style> 

     <Style TargetType="Button"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="Button"> 
         <Border Style="{StaticResource stlNavButtonBorder}" x:Name="border"> 
          <VisualStateManager.VisualStateGroups> 
           <VisualStateGroup x:Name="CommonStates"> 
            <VisualStateGroup.Transitions> 
             <VisualTransition GeneratedDuration="0:0:0.2"/> 
            </VisualStateGroup.Transitions> 
            <VisualState x:Name="Normal"/> 
            <VisualState x:Name="Pressed"> 
             <Storyboard> 
              <ColorAnimation Storyboard.TargetProperty="(BorderBrush).(SolidColorBrush.Color)" 
                   To="Black"/> 
             </Storyboard> 
            </VisualState> 
            <VisualState x:Name="MouseOver"> 
             <Storyboard> 
              <ColorAnimation Storyboard.TargetProperty="(BorderBrush).(SolidColorBrush.Color)" 
                  To="Blue"/> 
             </Storyboard> 
            </VisualState> 
           </VisualStateGroup> 
          </VisualStateManager.VisualStateGroups> 

          <Grid> 
           <Rectangle Style="{StaticResource stlNavButtonRectangle}"/> 
           <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> 
          </Grid> 
         </Border>       
        </ControlTemplate>       
       </Setter.Value> 
      </Setter> 
     </Style> 
    </StackPanel.Resources> 

    <Button Content="Button 1" /> 
    <Button Content="Button 2"/> 
    <Button Content="Button 3" /> 
</StackPanel> 

下面的代碼也可以除的情況下點擊按鈕,後,當我們離開按鈕鼠標進入後,它仍然是黑色

<StackPanel> 
    <StackPanel.Resources> 
     <Style x:Key="stlNavButtonBorder" TargetType="Border"> 
      <Setter Property="BorderBrush" Value="#570000FF" /> 
      <Setter Property="BorderThickness" Value="5" /> 
      <Setter Property="Height" Value="100" /> 
      <Setter Property="Width" Value="200" /> 
      <Setter Property="Margin" Value="10" /> 

     </Style> 
     <Style x:Key="stlNavButtonRectangle" TargetType="Rectangle"> 
      <Setter Property="Fill" Value="#570000FF"/> 
     </Style> 

     <Style TargetType="Button"> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="Button"> 
         <Border Style="{StaticResource stlNavButtonBorder}" x:Name="border"> 
          <Grid> 
           <Rectangle Style="{StaticResource stlNavButtonRectangle}"/> 
           <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> 
          </Grid> 
         </Border> 
         <ControlTemplate.Triggers>        
          <Trigger Property="IsMouseOver" Value="True"> 
           <Trigger.EnterActions> 
            <BeginStoryboard> 
             <Storyboard> 
              <ColorAnimation Storyboard.TargetName="border" 
               Storyboard.TargetProperty="BorderBrush.Color" 
               To="Blue" 
               Duration="0:0:0.25"/>    
             </Storyboard> 
            </BeginStoryboard> 
           </Trigger.EnterActions> 
           <Trigger.ExitActions> 
            <BeginStoryboard> 
             <Storyboard> 

              <ColorAnimation Storyboard.TargetName="border" 
               Storyboard.TargetProperty="BorderBrush.Color" 
               To="#570000FF" 
               Duration="0:0:0.25"/> 
             </Storyboard> 
            </BeginStoryboard> 
           </Trigger.ExitActions> 
          </Trigger> 
          <Trigger Property="IsPressed" Value="True"> 
           <Trigger.EnterActions> 
            <BeginStoryboard> 
             <Storyboard> 
              <ColorAnimation Storyboard.TargetName="border" 
               Storyboard.TargetProperty="BorderBrush.Color" 
               To="Black" 
               Duration="0:0:0.25" /> 
             </Storyboard> 
            </BeginStoryboard> 
           </Trigger.EnterActions> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate>       
       </Setter.Value> 
      </Setter> 
     </Style> 
    </StackPanel.Resources> 

    <Button Content="Button 1" /> 
    <Button Content="Button 2"/> 
    <Button Content="Button 3" /> 
</StackPanel> 
+0

非常感謝。我已經試過你的代碼,現在當點擊按鈕時,邊界確實變成黑色,但即使鼠標離開時它仍然保持黑色。非常感謝您的幫助! – Sipo

+0

@Sipo你想回到默認'MouseLeave'? – Gopichandar

+0

是的,在我的原始代碼中,它可以工作,但點擊時不會變黑。我試過你更新過的代碼,並且它可以工作,除了點擊按鈕之後,當我離開它時,它仍然是黑色的。非常感謝! – Sipo

-1

我檢查你的代碼,

MouseLeave事件正在發生,只是改變了顏色#570000FF

+1

當鼠標離開時,我確實希望顏色是'#570000f',但當鼠標關閉時,我希望它是黑色的。所以我不想改變'MouseLeave'事件的顏色。 – Sipo

+0

嗨,你已經給出了填充顏色(),並且在鼠標離開時你給出了相同的顏色,那麼你如何看到差異 – SiddarthVarunesh

+1

首先,顏色是'#570000f',然後當鼠標進入時它變成藍色,然後當它離開時,它返回到'#570000f' ... – Sipo

相關問題