wpf 控件綁定鼠標命令、鍵盤命令

 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>
前端代碼
 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 }
後的代碼
 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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章