首页 > 其他分享 >WPF KeyDown MVVM via CallMethodAction of behavior

WPF KeyDown MVVM via CallMethodAction of behavior

时间:2024-08-06 23:41:00浏览次数:9  
标签:CallMethodAction via idx MVVM Windows System private using public

 <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

相关文章

  • WPF DataGrid Checkbox column to implement select and unselect items explicitly v
    <DataGridTemplateColumnHeader="Select"><DataGridTemplateColumn.CellTemplate><DataTemplate><CheckBoxIsThreeState="False"><behavior:Interaction.Triggers>......
  • WPF locate discreted points via periodically and set transparency via the alpha,
    //xaml<Windowx:Class="WpfApp229.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • Android mvvm使用流程
    Androidmvvm使用流程一.几种常见架构模式对比1.MVC(Model-View-Controller)MVC模式的优点在于模块化、可扩展性和可维护性,但缺点是控制器和视图之间的耦合度较高。2.MVP(Model-View-Presenter)模式Presenter同时持有Model和View对象,缺点是V层与P层还是有一定的耦合度3.MVVM(Model-View......
  • PrismMVVM功能实现(通知、命令)
    常见的MVVM框架,基本围绕ICommand、INotifyPropertyChanged的封装实现绑定、通知等功能;而对于不同框架,在实现相同功能上,只是表现的形式有所不同,下图列举几种常见框架的功能区别:功能\框架PrismMVVMLightMicorsoft.Tookit.Mvvm通知BindableBaseViewModelBaseObservable......
  • 一个wpf项目的搭建prism框架mvvm
    一个wpf项目的搭建prism框架mvvm简单prism项目:1.新建一个wpf的项目2.引入包:在nuget中,prism.DryIoc3.把空项目应用转化成Prism,把App基类Application改为PrismApplication,因为这个基类是分布类,其中app.xaml.cs基类改为PrismApplication,和xaml的标签,引入命名空间后改为<prism......
  • Towards Practical Binary Code Similarity Detection: Vulnerability Verification v
    "迈向实用的二进制代码相似性检测:通过补丁语义分析进行漏洞验证"0x0Abstruct二进制代码相似性检测方法可以有效地搜索二进制软件中代码共享引入的重复出现的漏洞(1day)。然而,这些方法存在较高的误报率(FPR),因为它们通常将修补的函数视为易受攻击的函数,并且当使用不同的编译设置编译......
  • 《NET CLR via C#》---第四章(System.Object,类型转换,is和as,命名空间和程序集,运行时的相
    System.ObjectCLR要求每个类型最终都从System.Object类型派生。由于所有类型最终都从System.Object派生,所以每个类型的每个对象都保证了一组最基本的方法。公共方法说明Equals如果两个对象具有相同的值,就返回trueGetHashCode返回对象的值的哈希码。如果某个类型的......
  • 《NET CLR via C#》---第三章("运行时"解析类型引用)
    "运行时"解析类型引用首先在"C:\Users\LH89\source\repos"目录下,新建Console1工程(C#控制台)实现简单的代码,并编译为程序集(假定名为Program.exe)usingSystem;publicclassProgram{staticvoidMain(string[]args){Console.WriteLine("HelloWorld"......
  • X-Frame-Options may only be set via an HTTP header sent along with a documen
    X-Frame-OptionsmayonlybesetviaanHTTPheadersentalongwithadocumen_百度搜索(baidu.com)X-Frame-Options-盼星星盼太阳-博客园(cnblogs.com)vue项目中iframe嵌套其他项目,iframe父子页面传值-盼星星盼太阳-博客园(cnblogs.com)......
  • WPF TextBox not allowed illegal characters to be input and limit the text length
    //xaml<Windowx:Class="WpfApp224.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......