<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); } } }
标签:CallMethodAction,via,idx,MVVM,Windows,System,private,using,public From: https://www.cnblogs.com/Fred1987/p/18346177