wpf 控件绑定鼠标命令、键盘命令

摘要:
1˂Windowx:Class=“CommandDemo.MainWindow”2xmlns=“http://schemas.microsoft.com/winfx/2006/xaml/presentation“3xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml“4xmlns:d=”http://schemas.microsoft.com/

wpf 控件绑定鼠标命令、键盘命令第1张

wpf 控件绑定鼠标命令、键盘命令第2张wpf 控件绑定鼠标命令、键盘命令第3张
 1 <Window x:Class="CommandDemo.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 5         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 6         xmlns:local="clr-namespace:CommandDemo"
 7         mc:Ignorable="d"
 8         Title="MainWindow" Height="450" Width="800">
 9     <Grid>
10         <StackPanel Margin="10">
11             <TextBox Text="{Binding Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Height="40" />
12             <TextBlock Text="键盘tab,可以让Border获得焦点" />
13             <Border Width="100" Height="100" Background="Red" Focusable="True">
14                 <Border.InputBindings>
15                     <!--绑定鼠标左键双击-->
16                     <MouseBinding MouseAction="LeftDoubleClick" Command="{Binding DoubleClickCommand}" CommandParameter="{Binding}" />
17                     
18                     <!--绑定获取键盘焦点时,按下键盘组合键Ctrl+Enter时触发-->
19                     <KeyBinding Key="Enter" Modifiers="Control" Command="{Binding KeyCommand}" CommandParameter="{Binding}" />
20                 </Border.InputBindings>
21             </Border>
22         </StackPanel>
23     </Grid>
24 </Window>
前端代码
wpf 控件绑定鼠标命令、键盘命令第4张wpf 控件绑定鼠标命令、键盘命令第5张
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Windows;
 7 using System.Windows.Controls;
 8 using System.Windows.Data;
 9 using System.Windows.Documents;
10 using System.Windows.Input;
11 using System.Windows.Media;
12 using System.Windows.Media.Imaging;
13 using System.Windows.Navigation;
14 using System.Windows.Shapes;
15 
16 namespace CommandDemo
17 {
18     /// <summary>
19     /// MainWindow.xaml 的交互逻辑
20     /// </summary>
21     public partial class MainWindow : Window
22     {
23         public MainWindow()
24         {
25             InitializeComponent();
26             DataContext = new MainViewModel();
27         }
28     }
29 }
后的代码
wpf 控件绑定鼠标命令、键盘命令第6张wpf 控件绑定鼠标命令、键盘命令第7张
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 using System.Windows;
 8 using System.Windows.Input;
 9 
10 namespace CommandDemo
11 {
12     class MainViewModel : INotifyPropertyChanged
13     {
14         private string name;
15         public event PropertyChangedEventHandler PropertyChanged;
16 
17         public MainViewModel()
18         {
19             DoubleClickCommand = new DoubleClickCommand();
20             KeyCommand = new KeyCommand();
21         }
22 
23         public string Name
24         {
25             get => name;
26             set
27             {
28                 name = value;
29                 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Name)));
30             }
31         }
32 
33         public ICommand DoubleClickCommand { get; private set; }
34         public ICommand KeyCommand { get; private set; }
35     }
36 
37     class DoubleClickCommand : ICommand
38     {
39         public event EventHandler CanExecuteChanged;
40 
41         public bool CanExecute(object parameter)
42         {
43             return true;
44         }
45 
46         public void Execute(object parameter)
47         {
48             if (parameter is MainViewModel vm)
49             {
50                 MessageBox.Show(vm.Name);
51             }
52             MessageBox.Show("DoubleClick");
53         }
54     }
55     class KeyCommand : ICommand
56     {
57         public event EventHandler CanExecuteChanged;
58 
59         public bool CanExecute(object parameter)
60         {
61             return true;
62         }
63 
64         public void Execute(object parameter)
65         {
66             if (parameter is MainViewModel vm)
67             {
68                 MessageBox.Show(vm.Name);
69             }
70             MessageBox.Show("Key");
71         }
72     }
73 }
vm

免责声明:文章转载自《wpf 控件绑定鼠标命令、键盘命令》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇ES6+转ES5(webpack+babel、指定多个js文件、自动注入)MODBUS串行通信协议详细说明下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

wpf 中设置DataGridTextColumn的文本对齐方式

DataGrid里面存在着像DataGridColumnHeader、DataGrid、DataGridCell等相关控件的样式设置,例如让一个DataGrid里面某一列的控件内容居中显示,例如: DataGridColumnHeader View Code <Style x:Key="ColumnHeaderStyle" TargetType="{...

在WPF中如何使用RelativeSource绑定

在WPF绑定的时候,指定绑定源时,有一种办法是使用RelativeSource。 这种办法的意思是指当前元素和绑定源的位置关系。 第一种关系: Self 举一个最简单的例子:在一个StackPanel中,有一个TextBlock。 <TextBlock FontSize="18" FontWeight="Bold" Margin="10"...

无法载入DLL 'sqlceme35.dll':找不到指定的模组。 ( 发生例外狀況於HRESULT: 0x8007007E )

SQL Server Compact + x64开发,遇到下列问题。 System.DllNotFoundException 未處理 System.DllNotFoundException未处理 Message="無法載入DLL 'sqlceme35.dll': 找不到指定的模組。 (發生例外狀況於HRESULT: 0x8007007E)" Messag...

T4教程1 T4模版引擎之基础入门

T4模版引擎之基础入门    额,T4好陌生的名字,和NuGet一样很悲催,不为世人所熟知,却又在背后默默无闻的奉献着,直到现在我们项目组的人除了我之外,其它人还是对其豪无兴趣,基本上是连看一眼都懒得看,可怜的娃啊。。。   T4(Text Template Transformation Toolkit)是微软官方在VisualStudio 2008中...

WindowsService实现邮件定时发送

1.修改private System.Windows.Forms.Timer timer1;为private System.Timers.Timer timer1;    修改this.timer1 = new System.Windows.Forms.Timer(this.components)为this.timer1 = new System.Time...

手把手教你 用 wpf 制作metro ProgressRing (Windows8 等待动画)

效果图: 还在羡慕metro的ProgressRing吗? wpf 也可以拥有 首先说下思路, 一共6个点围绕一直圆转,所以需要使用rotation动画 并且一直转下去。 那么下面的问题就好解决了。 首先是xaml 部分 我们需要实现旋转动画: 所以要用到这个: [html]view plaincopyprint? <DoubleAnima...