首页 > 其他分享 >WPF Window.InputBindings KeyBinding Key

WPF Window.InputBindings KeyBinding Key

时间:2024-08-06 22:54:28浏览次数:10  
标签:Windows System private Window InputBindings value using WPF public

//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> 

 

标签:Windows,System,private,Window,InputBindings,value,using,WPF,public
From: https://www.cnblogs.com/Fred1987/p/18346136

相关文章

  • WPFUI报错 - page does not have a parameterless constructor
    WPFUI报错pagedoesnothaveaparameterlessconstructor.IfyouareusingWpf.Ui.IPageServicedonotnavigateinitiallyanddon'tuseCacheorPrecache问题原因WPFUI中的NavigationView只支持导航页面的无参构造函数或含一个dataContext的有参构造函数。因为在View......
  • WPF DataGrid Checkbox column to implement select and unselect items explicitly v
    <DataGridTemplateColumnHeader="Select"><DataGridTemplateColumn.CellTemplate><DataTemplate><CheckBoxIsThreeState="False"><behavior:Interaction.Triggers>......
  • windows常用快捷键
    电脑常用快捷键视频:https://www.bilibili.com/video/BV12J41137hu/?p=10&t=18s快捷键组合Ctrl+C:复制Ctrl+V:黏贴Ctrl+A:全选Ctrl+X:剪切Ctrl+Z:撤消Ctrl+S:保存Alt+F4:关闭窗口Shift+delete:永久删除文件Win+R:打开“运行”Win+E:打开“此电脑”Ctrl+Shi......
  • Xmind2024支持多平台使用,包括Windows、Mac、iOS、等操作系统
    “Xmind2024”是Xmind公司推出的一款全新的思维导图软件,它集成了多种功能,包括智能导图、AI生成、语音输入等。这款产品旨在帮助用户更高效地整理思路,提高思维能力。让我们来了解一下Xmind2024的特点。它采用了全新的设计风格,界面简洁明了,操作便捷。同时,它还提供了丰富的模板......
  • Spark StructStreaming Window和Watermark
    SparkStructStreamingWindow和Watermark前面我们介绍了tructuredStreaming的计算模型与容错机制。深入理解这些基本原理,会帮我们开发流处理应用打下坚实的基础。在“流动的WordCount”那一讲,我们演示了在StructuredStreaming框架下,如何做流处理开发的一般流程。......
  • Windows 系统学习之路
    一、AD服务部署https://mp.weixin.qq.com/mp/appmsgalbum?action=getalbum&__biz=MzUxNjMwMzk4MQ==&scene=1&album_id=3528459070271471627&count=3#wechat_redirect二、MDT服务部署https://mp.weixin.qq.com/mp/appmsgalbum?action=getalbum&__biz=MzUxNjMwMzk4M......
  • 如何在 ARM64 上的 Windows 上安装 SciPy 和 Numpy
    我需要numpy和scipy来执行一些信号分析。有人成功做到这一点吗?(我有兴趣在本机运行它,而不是通过virtualenv)。我的最终目标是从使用numpy和scipy的python脚本构建一个exe,可以在WinPE中运行进行测试。我已经成功安装了python3.11.2,并且能够进行numpy安装,但它......
  • Windows 和 MacOS 上安装配置ADB(安卓调试桥)_android adb工具安装 mac
    一、Android调试桥(ADB)Android调试桥(ADB)是一款多功能命令行工具,它让你能够更便捷地访问和管理Android设备。使用ADB命令,你可以轻松执行以下操作网络安全重磅福利:入门&进阶全套282G学习资源包免费分享!在设备上安装、复制和删除文件;安装应用程序;录制设备屏幕或截......
  • Mac开发基础08-NSWindow(二)
    NSWindow其他使用和技巧NSWindow是macOS应用程序中用于显示和管理窗口的核心类。可用于创建、编辑和管理应用程序的窗口。1.自定义窗口的内容视图层级替换默认的内容视图NSWindow默认包含一个内容视图,你可以使用自定义内容视图来替换它。Objective-CNSView*customVie......
  • windows AD域控密码过期邮件通知迭代版本
    利用poweshell脚本在域控服务器上查找即将过期的账号,并邮件推送至用户和管理员针对windowsAD域控密码过期邮件通知-二乘八是十六-博客园(cnblogs.com)文章的升级版本脚本升级内容:对账号设置三种形式:即将过期、已经过期、未激活三种状态进行通知对密码过期时间进行......