首页 > 其他分享 >WPF KeyDown MVVM Via Behavior

WPF KeyDown MVVM Via Behavior

时间:2024-08-15 20:15:54浏览次数:7  
标签:Via Windows System KeyDown MainWindow Behavior using WPF public

 <behavior:Interaction.Triggers>
     <behavior:EventTrigger EventName="KeyDown">
         <behavior:CallMethodAction MethodName="Window_KeyDown" TargetObject="{Binding}"/>
     </behavior:EventTrigger>
 </behavior:Interaction.Triggers>

 

 

 

//xaml
<Window x:Class="WpfApp248.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:WpfApp248"
        Title="MainWindow" Height="450" Width="800">
    <behavior:Interaction.Triggers>
        <behavior:EventTrigger EventName="KeyDown">
            <behavior:CallMethodAction MethodName="Window_KeyDown" TargetObject="{Binding}"/>
        </behavior:EventTrigger>
    </behavior:Interaction.Triggers>    
    <Grid>
        
    </Grid>
</Window>


//xaml.cs
using System;
using System.Collections.Generic;
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 WpfApp248
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var vm=new MainVM();
            this.DataContext = vm;
        }  
    }

    public class MainVM
    {
        public MainVM()
        { 
        }
         
        public void Window_KeyDown(object sender, KeyEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine(e.Key.ToString());
        }
    }
}

 

标签:Via,Windows,System,KeyDown,MainWindow,Behavior,using,WPF,public
From: https://www.cnblogs.com/Fred1987/p/18361751

相关文章

  • WPF Customize control
    //xaml<UserControlx:Class="WpfApp246.EllipseTbk"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc=&q......
  • 《NET CLR via C#》---第七章(常量,读/写字段,可读字段)
    常量常量是值从不变化的符号。定义常量符号时,它的值必须能在编译时确定。确定后,编译器将常量值保存到程序集元数据中。这意味着只能定义编译器识别的基元类型的常量。在C#中,可用于定义常量:Boolean,Char,Byte,SByte,Int16,UInt16,Int32,UInt32,Int64,UInt64,Single,Double,Decimal,String。C#也......
  • 《NET CLR via C#》---第六章(类型成员,类型的可访问性,友元程序集,分部类型,CLR调用方法指
    类型成员类型可以定义0个或者多个以下种类的成员。成员描述常量常量是指出数据值恒定不变的符号。这种符号使代码更易阅读和维护。常量总与类型关联,不与类型的实例关联。常量总与类型关联,不与类型的实例关联字段字段表示只读或可读/可写的数据值。字段可以是静态......
  • WPF-实现多语言的静态(需重启)与动态切换(不用重启)
    目录一、多语言切换(需重启)1、配置文件添加Key2、新增附加属性当前选择语言3、创建资源文件 4、初始化多语言集合5、切换多语言并更新配置文件6、应用程序启动根据配置切换多语言 7、使用二、多语言切换(无需重启)1、创建多语言标记扩展基类2、添加资源转换器3、创......
  • CAD二次开发入门:WPF类库
     参考学习视频:https://www.bilibili.com/video/BV16Y411v7kr/?spm_id_from=333.337.search-card.all.click&vd_source=fbb64ea20b269b753497bf6c2499fc29 第一步:创建WPF类库,并写CAD调用方法  修改输出类型为:类库 添加CAD开发需要的类库 main页面添加以下内容:......
  • wpf ValidationRule 校验数据输入
    publicclassCountValidationRule:ValidationRule{publicoverrideValidationResultValidate(objectvalue,System.Globalization.CultureInfocultureInfo){doubled=0.0;if(double.TryParse((string)value,out......
  • 【WPF】【XAML】Exception: 无法找到名为“xx”的资源。资源名称区分大小写。
    <Grid.Resources>一定要放在使用之前,比如<ListBoxName="peopleListBox"Grid.Column="1"Grid.Row="2"ItemsSource="{BindingSource={StaticResourceExpenseDataSource},XPath=Person}"ItemTemplate="{StaticResour......
  • wpf 如何写一个圆形的进度条
    先看一下效果吧调用代码如下<local:CycleProgressBarWidth="100"Height="100"Background="#FFF68986"Foreground="#FFFA1F09"Maximum="100"Minimum="0"Value="20"IsIndeter......
  • wpf GMap
    AMapProviderBasepublicabstractclassAMapProviderBase:GMapProvider{publicAMapProviderBase(){MaxZoom=null;RefererUrl="http://www.amap.com/";//Copyright=string.Format("©{0}高德Corporation,©{0}......
  • 《NET CLR via C#》---第五章(基元类型,引用类型和值类型,对象相等性和同一性,对象哈希码)
    基元类型编译器直接支持的数据类型称为基元类型(primitvietype),基元类型直接映射到Framework类库(FCL)中存在的类型。例如,C#的int直接映射到System.Int32类型。因此,以下4行代码都能正确编译,并生成完全相同的IL:inta1=0;//最方便的语法System.Int3......