WPF ListBoxItem双击、单击事件分别处理

 1      private void listBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
 2         {            
 3             var element = (FrameworkElement)sender;
 4             if (e.ClickCount == 1)
 5             {
 6                 var timer = new System.Timers.Timer(500);
 7                 timer.AutoReset = false;
 8                 timer.Elapsed += new ElapsedEventHandler((o, ex) => element.Dispatcher.Invoke(new Action(() =>
 9                 {
10                     var timer2 = (System.Timers.Timer)element.Tag;
11                     timer2.Stop();
12                     timer2.Dispose();
13                     UIElement_Click(element, e);
14                 })));
15                 timer.Start();
16                 element.Tag = timer;
17             }
18             if (e.ClickCount > 1)
19             {
20                 var timer = element.Tag as System.Timers.Timer;
21                 if (timer != null)
22                 {
23                     timer.Stop();
24                     timer.Dispose();
25                     UIElement_DoubleClick(sender, e);
26                 }
27             }
28         }
29 
30         private void UIElement_Click(object sender, MouseButtonEventArgs e)
31         {
32             
33         }
34 
35         private void UIElement_DoubleClick(object sender, MouseButtonEventArgs e)
36         {
37             
38         }

 

转载于:https://www.cnblogs.com/Eric-Hwang/p/8487209.html

WPF中,可以通过使用命令和交互行为来实现DataGrid的双击事件。首先,在XAML文件中导入命名空间`xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"`。然后,在DataGrid下面添加以下代码: ```xml <DataGrid ItemsSource="{Binding DeviceInfos}"> <i:Interaction.Triggers> <i:EventTrigger EventName="MouseDoubleClick"> <i:InvokeCommandAction Command="{Binding Path=DoubleClickItem}" CommandParameter="{Binding ElementName=da1,Path=SelectedItem}"/> </i:EventTrigger> </i:Interaction.Triggers> </DataGrid> ``` 这段代码将DataGrid的`MouseDoubleClick`事件与一个命令`DoubleClickItem`绑定起来。当双击DataGrid中的某一项时,命令将被执行,并将选中的项作为参数传递给命令。这样就实现了DataGrid的双击事件。\[2\]\[3\] #### 引用[.reference_title] - *1* [wpf datagrid 行双击事件](https://blog.csdn.net/dieruowan6112/article/details/101202876)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [WPF datagrid双击事件获取双击的对象或值MVVM](https://blog.csdn.net/weixin_38083655/article/details/109187564)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值