WPF InputBinding(KeyBinding、MouseBinding)

参考

环境

软件/系统 版本 说明
Windows windows 10 专业版 22H2 64 位操作系统, 基于 x64 的处理器
Microsoft Visual Studio Community 2022 (64 位) - Current 版本 17.13.6
.NET Framework 4.8

部分代码

  1. MainWindow.xaml
    <Window
    x:Class="WPFInputBinding.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:WPFInputBinding"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:vm="clr-namespace:WPFInputBinding.ViewModel"
    Title="MainWindow"
    Width="800"
    Height="450"
    mc:Ignorable="d">
    	<Window.DataContext>
    		<vm:MainWindowVM />
    	</Window.DataContext>
    	<Window.InputBindings>
    		<!--  按键绑定  -->
    		<KeyBinding Command="{Binding KeyBindCommand}" Gesture="Ctrl+R" />
    		<KeyBinding Key="B" Command="{Binding KeyBindCommand}" />
    		<!--  鼠标操作绑定  -->
    		<MouseBinding Command="{Binding MouseBindCommand}" MouseAction="LeftClick" />
    		<MouseBinding Command="{Binding MouseBindCommand}" MouseAction="MiddleClick" />
    	</Window.InputBindings>
    	<StackPanel>
    		<TextBox x:Name="InputText" Text="数据绑定">
    			<TextBox.InputBindings>
    				<!--  控件内的按键绑定  -->
    				<KeyBinding Key="B" Command="ApplicationCommands.Paste" />
    				<!--  控件内的鼠标操作绑定  -->
    				<MouseBinding Command="{Binding MouseBindCommand}" MouseAction="MiddleClick" />
    			</TextBox.InputBindings>
    		</TextBox>
    		<TextBlock Text="{Binding ElementName=InputText, Path=Text}" />
    		<Separator />
    		<Button Command="{Binding CommandBindTextCommand}" Content="{Binding CommandBindText}" />
    		<Separator />
    
    	</StackPanel>
    </Window>
    
    
    
  2. MainWindowVM.cs
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Input;
    using WPFInputBinding.Utilities;
    
    namespace WPFInputBinding.ViewModel
    {
    	public class MainWindowVM : Utilities.ViewModelBase
    	{
    		/// <summary>
    		/// 输入绑定
    		/// </summary>
    		private string _commandBindText { get; set; } = "命令绑定";
    		public string CommandBindText
    		{
    			get { return _commandBindText; }
    			set { _commandBindText = value; OnPropertyChanged(); }
    		}
    		/// <summary>
    		/// 点击绑定
    		/// </summary>
    		public ICommand CommandBindTextCommand { get; set; }
    		private void CommandBindTextEvent(object obj)
    		{
    			CommandBindText = "已点击";
    		}
    
    		/// <summary>
    		/// 按键绑定
    		/// </summary>
    		public ICommand KeyBindCommand { get; set; }
    		private void KeyBindEvent(object obj)
    		{
    			Debug.WriteLine("按下了快捷键");
    		}
    		/// <summary>
    		/// 鼠标绑定
    		/// </summary>
    		public ICommand MouseBindCommand { get; set; }
    		private void MouseBindEvent(object obj)
    		{
    			if(obj is MouseAction mouseAction)
    			{
    				Debug.WriteLine(mouseAction.ToString());
    			}
    			Debug.WriteLine("鼠标操作");
    		}
    
    		public MainWindowVM()
    		{
    			CommandBindTextCommand = new RelayCommand(CommandBindTextEvent);
    			KeyBindCommand = new RelayCommand(KeyBindEvent);
    			MouseBindCommand = new RelayCommand(MouseBindEvent);
    		}
    
    	}
    }
    
    
posted @ 2025-05-29 11:45  夏秋初  阅读(43)  评论(0)    收藏  举报