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));
    }
}





 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值