wpf RoutedUICommand 绑定

简介: 如果 自己设置按钮的快捷键就用后台绑定 读取自己设置的快捷键见方法2   方法1  ...

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

读取自己设置的快捷键见方法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));
    }
}

  

目录
相关文章
WPF—多重绑定和跨层级绑定
WPF—多重绑定和跨层级绑定
|
C# 数据格式 XML
WPF 资源(StaticResource 静态资源、DynamicResource 动态资源、添加二进制资源、绑定资源树)
原文:WPF 资源(StaticResource 静态资源、DynamicResource 动态资源、添加二进制资源、绑定资源树) 一、WPF对象级(Window对象)资源的定义与查找 实例一: StaticR...
8392 0
|
C# 数据库
WPF中DataGrid控件绑定数据源
WPF中DataGrid控件绑定数据源
163 0
|
C#
WPF更新绑定字段
WPF更新绑定字段
99 0
|
前端开发 C#
WPF 之 数据与命令绑定 (MVVM方式)
WPF 之 数据与命令绑定 (MVVM方式)
195 0
WPF 之 数据与命令绑定 (MVVM方式)
|
C#
WPF 绑定父类属性
原文:WPF 绑定父类属性 1.绑定父控件的属性. 1 2 3 4 5 6 7 8 9 发现问题,父控件的属性如果是后期加载的,别如说Width或者Height不是固定的数值,那么绑定时没有效果的。
1271 0
|
C# 前端开发
wpf中的datagrid绑定操作按钮是否显示或者隐藏
如图,需要在wpf中的datagrid的操作那列有个确认按钮,然后在某些条件下确认按钮可见,某些情况下不可见的,放在mvc里直接在cshtml页面中if..else就行了。 但是在wpf里不行。。网上搜索了好久才找到解决方法,原来只是binding那个visiable属性就行了,
6891 0
|
C#
【WPF】绑定Hyperlink超链接
原文:【WPF】绑定Hyperlink超链接 Hyperlink超链接的简单使用 前台XAML: 说明文字: www.baidu.com 后台代码实现点击超链接的逻辑: private void Hyper...
1414 0
|
C#
WPF的5种绑定模式(mode)
原文:WPF的5种绑定模式(mode) WPF的绑定模式(mode)是枚举的 枚举值共有5个 1:OneWay(源变就更新目标属性) 2:TwoWay(源变就更新目标并且目标变就更新源) 3:OneTime(只根据源来设置目标,以后都不会变) 4:OneWayToSource(与OneWay相反) 5:Default(可以单向或双向,是靠被值定的源或目标是否有get或set来指定的) 所以绑定的话是需要选上面5个中的一个模式的,根据你的需要来选择,不选的话就会自动选择第五个的。
1162 0
|
C# .NET 开发框架
WPF笔记 ( xmlns引用,Resource、Binding 前/后台加载,重新绑定) 2013.6.7更新
原文:WPF笔记 ( xmlns引用,Resource、Binding 前/后台加载,重新绑定) 2013.6.7更新 1、xmlns Mapping URI的格式是 clr-namespace:[;assembly=] (1)如果自定义类和XAML处在同一个Assembly之中,只还需要提供clr-namespace值。
1446 0