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

  

posted @ 2012-12-20 17:59  li-peng  阅读(2901)  评论(0编辑  收藏  举报