1- using System ;
2- using System . Windows ;
1+ using System . Windows ;
32using System . Windows . Controls ;
3+ using System . Windows . Input ;
44using System . Windows . Media ;
5- using System . Windows . Media . Animation ;
65using System . Windows . Shapes ;
76
87using HandyControl . Controls ;
98
10- using IPConfig . Languages ;
11-
129using Microsoft . Xaml . Behaviors ;
1310
1411namespace IPConfig . Behaviors ;
@@ -18,7 +15,11 @@ namespace IPConfig.Behaviors;
1815/// </summary>
1916public class SplitButtonToggleDropDownBehavior : Behavior < SplitButton >
2017{
21- private bool _canPlayTextAnimation ;
18+ protected virtual void OnAssociatedObjectClick ( object sender , RoutedEventArgs e )
19+ { }
20+
21+ protected virtual void OnAssociatedObjectPreviewMouseLeftButtonDown ( object sender , MouseButtonEventArgs e , HitTestResult hitTestResult )
22+ { }
2223
2324 protected override void OnAttached ( )
2425 {
@@ -36,51 +37,22 @@ protected override void OnDetaching()
3637 base . OnDetaching ( ) ;
3738 }
3839
39- private void AnimateText ( )
40- {
41- NameScope . SetNameScope ( AssociatedObject , new NameScope ( ) ) ;
42- string name = $ "SplitButton_{ Guid . NewGuid ( ) . ToString ( ) . Replace ( '-' , '_' ) } ";
43- AssociatedObject . RegisterName ( name , AssociatedObject ) ;
44-
45- var keyFrames = new StringAnimationUsingKeyFrames {
46- Duration = new ( TimeSpan . FromSeconds ( 1 ) )
47- } ;
48-
49- var frame = new DiscreteStringKeyFrame ( Lang . Copied , KeyTime . FromTimeSpan ( TimeSpan . Zero ) ) ;
50- keyFrames . KeyFrames . Add ( frame ) ;
51-
52- var story = new Storyboard { FillBehavior = FillBehavior . Stop } ;
53- Storyboard . SetTargetName ( keyFrames , name ) ;
54- Storyboard . SetTargetProperty ( keyFrames , new PropertyPath ( SplitButton . ContentProperty ) ) ;
55- story . Children . Add ( keyFrames ) ;
56- story . Begin ( AssociatedObject ) ;
57- }
58-
5940 private void AssociatedObject_Click ( object sender , RoutedEventArgs e )
6041 {
61- if ( _canPlayTextAnimation )
62- {
63- AnimateText ( ) ;
64-
65- _canPlayTextAnimation = false ;
66- }
42+ OnAssociatedObjectClick ( sender , e ) ;
6743 }
6844
69- private void AssociatedObject_PreviewMouseLeftButtonDown ( object sender , System . Windows . Input . MouseButtonEventArgs e )
45+ private void AssociatedObject_PreviewMouseLeftButtonDown ( object sender , MouseButtonEventArgs e )
7046 {
71- var result = VisualTreeHelper . HitTest ( AssociatedObject , e . GetPosition ( AssociatedObject ) ) ;
72-
73- if ( result ? . VisualHit is Border { Child : null } or TextBlock )
74- {
75- _canPlayTextAnimation = true ;
76- }
47+ var hitTestResult = VisualTreeHelper . HitTest ( AssociatedObject , e . GetPosition ( AssociatedObject ) ) ;
48+ OnAssociatedObjectPreviewMouseLeftButtonDown ( sender , e , hitTestResult ) ;
7749
7850 // 如果 VisualHit 为 Path,则鼠标在点击 SplitButton 的 DropDown 位置,
7951 // 如果为 TextBlock,则鼠标在点击 SplitButton 的非 DropDown 位置。
8052 // 如果为 Border,并且其 Child 不为 null,则鼠标在点击 DropDown 位置,否则,鼠标在点击 非 DropDown 位置。
8153 // 如果为 null,则鼠标在点击其他位置。
8254 // 如果不判断 VisualHit,则在点击下拉项前,此事件将被调用,下拉项被关闭,导致下拉项无法被点击到。
83- if ( AssociatedObject . IsDropDownOpen && result ? . VisualHit is Border or Path or TextBlock )
55+ if ( AssociatedObject . IsDropDownOpen && hitTestResult ? . VisualHit is Border or Path or TextBlock )
8456 {
8557 AssociatedObject . IsDropDownOpen = false ;
8658 e . Handled = true ;
0 commit comments