• 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
  • 相关阅读:
    暑假第二周总结
    7.18-7.24 第一周周报
    poj 3295 Tautology
    2016多校 #2 1006 Fantasia
    codeforces 698B Fix a Tree
    codeforces 699B Bomb
    HDU 4578(线段树
    CF 600F( 二分图
    hdu 5517 Triple(二维树状数组)
    HDU HDOJ5412(树套树or整体二分
  • 原文地址:https://www.cnblogs.com/xuling-297769461/p/15014406.html
Copyright © 2020-2023  润新知