简单实现鼠标移过Popup自动显示和隐藏的功能;
在xaml.cs中实现,首先创建一个定时器,设置300ms的延时:
1 DispatcherTimer timer; 2 3 public Function1View() 4 { 5 InitializeComponent(); 6 7 timer = new DispatcherTimer(); 8 timer.Tick += timer_Tick; 9 timer.Interval = TimeSpan.FromMilliseconds(300); 10 }
z定时器的触发函数中,隐藏Popup:
1 void timer_Tick(object sender, EventArgs e) 2 { 3 menuPopup.IsOpen = false; 4 }
Popup和目标控件的鼠标进入和离开函数:
1 /// <summary> 2 /// 鼠标移走 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 private void Border_MouseLeave(object sender, MouseEventArgs e) 7 { 8 timer.Start(); 9 } 10 /// <summary> 11 /// 鼠标移走 12 /// </summary> 13 /// <param name="sender"></param> 14 /// <param name="e"></param> 15 private void menuPopup_MouseLeave(object sender, MouseEventArgs e) 16 { 17 timer.Start(); 18 } 19 /// <summary> 20 /// 鼠标进入 21 /// </summary> 22 /// <param name="sender"></param> 23 /// <param name="e"></param> 24 private void Border_MouseEnter(object sender, MouseEventArgs e) 25 { 26 menuPopup.IsOpen = true; 27 timer.Stop(); 28 } 29 /// <summary> 30 /// 鼠标进入 31 /// </summary> 32 /// <param name="sender"></param> 33 /// <param name="e"></param> 34 private void menuPopup_MouseEnter(object sender, MouseEventArgs e) 35 { 36 menuPopup.IsOpen = true; 37 timer.Stop(); 38 }
实现效果: