• wpf RoutedUICommand 绑定快捷键


    如果 自己设置按钮的快捷键就用后台绑定

    读取自己设置的快捷键见方法2

    方法1 

    <Window x:Class="CustomerShortcutsDemo.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
        <Window.Resources>
     
            <RoutedUICommand x:Key="btnClick1" Text="Button Click"/>
     
        </Window.Resources>
     
        <Window.InputBindings>
     
            <KeyBinding Key="I" Modifiers="Ctrl + Alt"  Command="{StaticResource btnClick1}"/>
     
        </Window.InputBindings>
     
        <Window.CommandBindings>
     
            <CommandBinding Command="{StaticResource btnClick1}"
     
                            CanExecute="CommandBinding_CanExecute1"
     
                            Executed="CommandBinding_Executed1"/>
     
        </Window.CommandBindings>
     
        <Grid>
     
            <Button Content="Button 1" Height="23" HorizontalAlignment="Left" Margin="102,38,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" >
     
            </Button>
     
        </Grid>
    </Window>

     后台

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    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 CustomerShortcutsDemo
    {
        /// <summary>
        /// Window1.xaml 的交互逻辑
        /// </summary>
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
            }
     
            private void button1_Click(object sender, RoutedEventArgs e)
            {
     
                //MessageBox.Show("God");
                CustomerShortTwo win_CustomerShortTwo=new CustomerShortTwo();
                win_CustomerShortTwo.Show();
     
     
            }
     
            private void CommandBinding_CanExecute1(object sender, CanExecuteRoutedEventArgs e)
            {
     
                e.CanExecute = true;
     
            }
     
            private void CommandBinding_Executed1(object sender, ExecutedRoutedEventArgs e)
            {
     
                button1_Click(this, null);
     
            }
        }
    }

      

    第二种后台绑定

    <Window x:Class="CustomerShortcutsDemo.CustomerShortTwo"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:CustomerShortcutsDemo"
        Title="CustomerShortTwo" Height="300" Width="300">
        <Window.CommandBindings>
            <CommandBinding Command="local:CommandBindingDEmo.myCommand"
                           CanExecute="cb_CanExecute"  Executed="myRoudedEvent" />
        </Window.CommandBindings>
     
        <Grid>
            <Menu Margin="40,107,96,122">
                <MenuItem Header="文件">
                    <MenuItem Name="MyIte"/>
                    <MenuItem Name="MyIteT"/>
                </MenuItem>
            </Menu>
            <Button Name="My_Button" Click="MyClick" Height="71" VerticalAlignment="Top" HorizontalAlignment="Left" Width="113"></Button>
        </Grid>
    </Window>

      

      后台

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    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.Shapes;
     
    namespace CustomerShortcutsDemo
    {
        /// <summary>
        /// CustomerShortTwo.xaml 的交互逻辑
        /// </summary>
        public partial class CustomerShortTwo : Window
        {                       
            public CustomerShortTwo()
            {
                InitializeComponent();
     
     
                MyIte.Command = CommandBindingDEmo.myCommand;
                //this.CommandBindings.Add(cb);
     
                this.InputBindings.Add(new KeyBinding(CommandBindingDEmo.myCommand, new KeyGesture(Key.I, ModifierKeys.Alt | ModifierKeys.Control)));
            }
     
            void cb_CanExecute(object sender, CanExecuteRoutedEventArgs e)
            {
                e.CanExecute = true;
            }
     
            public void myRoudedEvent(object sender, ExecutedRoutedEventArgs e)
            {
                MyClick(null, null);
            }
            public void MyClick(object sender, RoutedEventArgs e)
            {
     
                MessageBox.Show("Click!");
            }
        }
     
        public class CommandBindingDEmo
        {
            public static RoutedUICommand myCommand = new RoutedUICommand("Open", "ClickOpen", typeof(CommandBinding));
            public static RoutedUICommand myCommandTwo = new RoutedUICommand("Close", "clickClose", typeof(CommandBinding));
        }
    }

      

    出处:https://www.cnblogs.com/li-peng/archive/2012/12/20/2826821.html

  • 相关阅读:
    如何处理CrashLoopBackOff状态的pod
    如何全方位的保障系统的稳定性
    Linux系统-网络带宽占用分析
    Docker之路-通过swarm管理配置及服务升级回滚
    Effective Java
    springboot websocket 简单入门
    JDK8 API文档
    Java反编译工具 Java Decompiler
    如何在高并发分布式系统中生成全局唯一ID
    Linux命令发送Http GET/POST请求
  • 原文地址:https://www.cnblogs.com/mq0036/p/12422520.html
Copyright © 2020-2023  润新知