WPF Window.InputBindings KeyBinding Key

FredGrit發表於2024-08-06
//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> 

相關文章