WPF Window.InputBindings KeyBinding Key
//xaml <Window x:Class="WpfApp233.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:behavior="http://schemas.microsoft.com/xaml/behaviors" xmlns:local="clr-namespace:WpfApp233" mc:Ignorable="d" WindowState="Maximized" Title="{Binding ImgUrl,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Height="450" Width="800"> <Window.InputBindings> <KeyBinding Command="{Binding KeyDownCmd}" Key="Down"/> <KeyBinding Command="{Binding KeyUpCmd}" Key="Up"/> </Window.InputBindings> <Grid> <Image Source="{Binding ImgUrl,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/> <TextBlock Text="{Binding Idx,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Right" VerticalAlignment="Bottom" Width="50" FontSize="30" Foreground="Cyan"/> </Grid> </Window> //cs using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApp233 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); var vm = new BookVM(); this.DataContext = vm; } } public class BookVM : INotifyPropertyChanged { public BookVM() { InitData(); InitCmds(); } private void InitCmds() { KeyDownCmd = new DelCmd(KeyDownCmdExecuted); KeyUpCmd = new DelCmd(KeyUpCmdExecuted); } private void KeyUpCmdExecuted(object obj) { --Idx; } private void KeyDownCmdExecuted(object obj) { ++Idx; } private void InitData() { imgsList =new List<string>(System.IO.Directory.GetFiles(@"../../Images")); imgsCount = imgsList.Count(); ImgUrl=imgsList[0]; } public DelCmd KeyDownCmd { get; set; } public DelCmd KeyUpCmd { get; set; } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { var handler = PropertyChanged; if (handler != null) { handler?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } private string winTitle; public string WinTitle { get { return winTitle; } set { if(value!=winTitle) { winTitle = value; OnPropertyChanged(nameof(WinTitle)); } } } private int idx = 0; public int Idx { get { return idx; } set { if(value!=idx) { idx = value; if(idx>imgsCount-1) { idx = 0; } if(idx<0) { idx=imgsCount-1; } ImgUrl = imgsList[idx]; OnPropertyChanged(nameof(Idx)); } } } private List<string> imgsList { get; set; } private int imgsCount { get; set; } private string imgUrl; public string ImgUrl { get { return imgUrl; } set { if(value!=imgUrl) { imgUrl = value; OnPropertyChanged(nameof(ImgUrl)); } } } } public class DelCmd : ICommand { public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } private Action<object> execute; private Predicate<object> canExecute; public DelCmd(Action<object> executeValue, Predicate<object> canExecuteValue) { execute = executeValue; canExecute = canExecuteValue; } public DelCmd(Action<object> execute) : this(execute, null) { } public bool CanExecute(object parameter) { if (canExecute == null) { return true; } return canExecute(parameter); } public void Execute(object parameter) { execute(parameter); } } }
<Window.InputBindings> <KeyBinding Command="{Binding KeyDownCmd}" Key="Down"/> <KeyBinding Command="{Binding KeyUpCmd}" Key="Up"/> </Window.InputBindings>
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· [翻译] 为什么 Tracebit 用 C# 开发
· Deepseek官网太卡,教你白嫖阿里云的Deepseek-R1满血版
· DeepSeek崛起:程序员“饭碗”被抢,还是职业进化新起点?
· 2分钟学会 DeepSeek API,竟然比官方更好用!
· .NET 使用 DeepSeek R1 开发智能 AI 客户端
2022-08-06 Vim: Warning: Output is not to a terminal to unlock the terminal
2022-08-06 Ubuntu search keyword via grep in specified file,the short cut to capture selection screen shot in Ubuntu 20.04 is Ctrl+Shift+Alt+R to start take screenshot functions