WPF KeyDown MVVM via CallMethodAction of behavior
<behavior:Interaction.Triggers> <behavior:EventTrigger EventName="KeyDown"> <behavior:CallMethodAction MethodName="WinKeyDown" TargetObject="{Binding}"/> </behavior:EventTrigger> </behavior:Interaction.Triggers>
<Window x:Class="WpfApp234.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:WpfApp234" mc:Ignorable="d" WindowState="Maximized" Title="{Binding ImgUrl,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Height="450" Width="800"> <behavior:Interaction.Triggers> <behavior:EventTrigger EventName="KeyDown"> <behavior:CallMethodAction MethodName="WinKeyDown" TargetObject="{Binding}"/> </behavior:EventTrigger> </behavior:Interaction.Triggers> <Grid> <Grid.Background> <ImageBrush ImageSource="{Binding ImgUrl,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/> </Grid.Background> </Grid> </Window> //cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; 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 WpfApp234 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); var vm = new WinVM(); this.DataContext = vm; } } public class WinVM : INotifyPropertyChanged { public WinVM() { InitData(); } public void WinKeyDown(object sender, KeyEventArgs e) { switch (e.Key) { case Key.Up: Idx++; break; case Key.Down: Idx--; break; default: break; } } private void InitData() { imgsList = new List<string>(System.IO.Directory.GetFiles(@"../../Images")); ImgUrl = imgsList[Idx]; imgsCount = imgsList.Count; } private List<string> imgsList { get; set; } private int imgsCount { get; set; } private int idx = 0; public int Idx { get { return idx; } set { if (value != idx) { idx = value; if (idx >= imgsCount) { idx = 0; } if (idx < 0) { idx = imgsCount - 1; } ImgUrl = imgsList[idx]; OnPropertyChanged(nameof(Idx)); } } } private string imgUrl; public string ImgUrl { get { return imgUrl; } set { if (value != imgUrl) { imgUrl = value; OnPropertyChanged(nameof(ImgUrl)); } } } public event PropertyChangedEventHandler PropertyChanged; private void OnPropertyChanged(string propertyName) { var handler = PropertyChanged; if (handler != null) { handler?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } } 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> executeValue) : this(executeValue, null) { } public bool CanExecute(object parameter) { if (canExecute == null) { return true; } return canExecute(parameter); } public void Execute(object parameter) { execute(parameter); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
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