首页 > 其他分享 >WPF window keydown

WPF window keydown

时间:2024-06-17 11:00:57浏览次数:17  
标签:Windows imgsList System keydown imgIndex window new using WPF

<Window x:Class="WpfApp175.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:local="clr-namespace:WpfApp175" KeyDown="Window_KeyDown"
        mc:Ignorable="d" WindowState="Maximized"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Image x:Name="img" ClipToBounds="True" />
    </Grid>
</Window>


//cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.OleDb;
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 WpfApp175
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window,INotifyPropertyChanged
    {         
        private List<string> imgsList{ get; set; }
        private int imgsCount { get; set; }
        private int imgIndex { get; set; } = 0;
        public int ImgIndex
        {
            get
            {
                if(imgIndex >= imgsCount)
                {
                    imgIndex = 0;
                }
                this.Title = imgsList[imgIndex];
                return imgIndex;
            }
            set
            {
                imgIndex = value;
                OnPropertyChanged(nameof(ImgIndex));                 
            }
        }
        public MainWindow()
        {
            InitializeComponent(); 
            this.DataContext = this;
            this.Loaded += MainWindow_Loaded;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propName)
        {
            var handler= PropertyChanged;
            if(handler!=null)
            {
                handler?.Invoke(this,new PropertyChangedEventArgs(propName));
            }
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            imgsList = new List<string>(System.IO.Directory.GetFiles(@"..\..\Images").ToList());
            img.Source = new BitmapImage(new Uri(imgsList[ImgIndex], UriKind.RelativeOrAbsolute));
            imgsCount = imgsList.Count; 
        } 
        
        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.Key==Key.Down)
            {
                if (++ImgIndex >= imgsCount)
                {
                    ImgIndex = 0;
                }
                string imgUrl = imgsList[ImgIndex];
                img.Source = new BitmapImage(new Uri(imgUrl, UriKind.RelativeOrAbsolute));  
            } 
        }
    }
}

 

标签:Windows,imgsList,System,keydown,imgIndex,window,new,using,WPF
From: https://www.cnblogs.com/Fred1987/p/18251969

相关文章

  • WPF press keydown and show image one by one
    //xaml<Windowx:Class="WpfApp175.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • WPF customized Image control can add watermark , save and restore
     //usercontrol//xaml<UserControlx:Class="WpfApp173.ImageZoomPanWatermark"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quo......
  • 在window 使用 docker 安装redis 踩坑记
    1.安装REDIS在安装的时候,使用dockerpullredis就可以了。但是实际上发现镜像居然拉不下来。修改了一下docker镜像。配置如下:"registry-mirrors":["https://dockerproxy.com","https://docker.m.daocloud.io","https://docker.mirrors.ustc.edu.cn&quo......
  • 如何在WIndows虚拟机安装 macOS 黑苹果系统?
    在本教程中,我们将介绍如何在虚拟机上安装macOS黑苹果系统。黑苹果系统是非苹果公司官方支持的macOS系统的非官方版本,可以在普通PC上运行。请注意,安装黑苹果系统可能违反苹果的许可协议,请自行承担风险。参考视频教程:https://www.youtube.com/watch?v=FO9Bwa5btn81......
  • visible_windows生成逻辑和解析
    visible_windows生成逻辑和解析visible_windows生成逻辑在开发者选项中生成错误报告或命令行使用dumpstate,会生成一个bugreprot的压缩包,如其中包含了一些dump信息,其中海有个压缩包里面包含了一些可见窗口的控件信息,但用notepad打开却多是乱码之前工作遇到过一个显示问题只有截图、......
  • Windows系统上安装部署苹果系统(Mac OS)的几种方法
    /*MacOS苹果系统,正常情况下,只能安装到苹果公司自己出品的Mac电脑,俗称白苹果,不能安装到各种组装机或者其他品牌的品牌机上,黑苹果的的原理,就是通过一些“破解补丁”工具欺骗macOS系统,让苹果系统认为你的电脑其实是一台苹果电脑,从而可以安装运行。*/1.购买苹果笔记本或苹果一......
  • Ubuntu server 24 (Linux) 安装部署samba服务器 共享文件目录 windows访问
    1安装sudoaptupdatesudoapt-getinstallsamba#启动服务sudosystemctlrestartsmbd.servicesudosystemctlenablesmbd.service#查看服务2创建用户#创建系统用户sudouseraddtest2#配置用户密码sudosmbpasswd-atest2#smbpasswd:-a添加用户-......
  • WPF修改MessageBox样式(.NET6版本)
    一、问题场景使用HandyControl简写HC作为基础UI组件库时,希望系统中所有的MessageBox样式都使用HC的MessageBox,常规操作如下:在对应的xxxx.cs顶部使用using指定特定类的命名空间。usingMessageBox=HandyControl.Controls.MessageBox;这样,当前页面对应的MessageBox就都......
  • windows系统python代码打包为exe
    目录1建立虚拟环境condavenv2打包安装pyinstaller打包1建立虚拟环境好处:打包文件小,成功率高。坏处:代码使用到的第三方库需要重新安装如果不想使用虚拟环境,直接看2打包。conda如果没有conda,直接使用venv。查看虚拟环境condaenvlist创建虚拟环境condacreate......
  • 全面的初级入门指南,从安装到基本使用,再到一些高级功能的介绍,帮助用户在实际操作中逐步
    大纲:WindowsNmap初级使用教程1.简介什么是Nmap?Nmap的主要功能和用途安全和法律注意事项2.安装Nmap前提条件从官方网站下载Nmap安装步骤验证安装3.基本使用打开命令提示符运行你的第一个Nmap扫描示例命令:nmap目标IP地址理解基本的输出结果4.常用扫......