WPF学习笔记“命令”三:执行命令

View Code
 1 <Window x:Class="Command.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         Title="MainWindow" Height="421" Width="787">
 5     <Window.CommandBindings>
 6         <CommandBinding Command="Open" Executed="BtnOpen"></CommandBinding>
 7     </Window.CommandBindings>
 8     <Grid Width="769">
 9         <GroupBox Header="普通绑定" Height="100" HorizontalAlignment="Left" Name="groupBox1" VerticalAlignment="Top" Width="200" Margin="28,199,0,0">
10             <Button Command="Open" Content="Open" Height="23" Name="button1" Width="75" />
11         </GroupBox>
12         <GroupBox Header="copy的第一种绑定方式" Height="168" HorizontalAlignment="Left" Margin="460,8,0,0" Name="groupBox2" VerticalAlignment="Top" Width="281">
13             <Grid>
14                 <Button Command="Copy" CommandTarget="{Binding ElementName=txtDocument}" Content="Copy" Height="23" HorizontalAlignment="Left" Margin="6,19,0,0" Name="button2" VerticalAlignment="Top" Width="75" />
15                 <Button Command="Cut" CommandTarget="{Binding ElementName=txtDocument}" Content="Cut" Height="23" HorizontalAlignment="Left" Margin="6,66,0,0" Name="button3" VerticalAlignment="Top" Width="75" />
16                 <Button Command="Paste" CommandTarget="{Binding ElementName=txtDocument}" Content="Paste" Height="23" HorizontalAlignment="Left" Margin="6,114,0,0" Name="button4" VerticalAlignment="Top" Width="75" />
17                 <TextBox Height="23" HorizontalAlignment="Left" Margin="122,20,0,0" Name="txtDocument" VerticalAlignment="Top" Width="120" />
18             </Grid>
19         </GroupBox>
20         <GroupBox Header="copy的第二种绑定" Height="150" HorizontalAlignment="Left" Margin="466,206,0,0" Name="groupBox3" VerticalAlignment="Top" Width="267">
21             <Grid>
22                 <ToolBar Height="26" HorizontalAlignment="Left" Margin="6,20,0,0" Name="toolBar1" VerticalAlignment="Top" Width="200" >
23                     <Button Command="Copy" Content="{Binding RelativeSource={RelativeSource Self},Path=Command.Text}"></Button>
24                     <Button Command="Cut" Content="{Binding RelativeSource={RelativeSource Self},Path=Command.Text}"></Button>
25                     <Button Command="Paste" Content="{Binding RelativeSource={RelativeSource Self},Path=Command.Text}"></Button>
26                 </ToolBar>
27                 <TextBox Height="23" HorizontalAlignment="Left" Margin="31,82,0,0" Name="txtDocumentSecond" VerticalAlignment="Top" Width="120" />
28             </Grid>
29         </GroupBox>
30     </Grid>
31 </Window>
View Code
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 Command
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void BtnOpen(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("You Click " + button1.Content + " Command");
        }
    }
}

  RoutedUICommand类没有任何硬编码的功能。它只是表示一个命令。为了触发命令,需要一个命令源。为了响应命令,需要一个命令绑定,该绑定将执行转发给一个普通的事件处理程序。

  触发命令最简单的方法是将命令关联到一个实现了ICommandSource接口的控件,其中包括继承自ButtonBase类的控件(butto、checkbox);

  ICommandSource接口定义了三个属性:

    Command:指向连接的命令,这是唯一必需的细节;

    CommandParameter:提供其他希望跟随命令发送的数据;

    CommandTarget:确定将要在其中执行命令的元素

一、普通的绑定方式

   <Button Command="Open" Content="Open" Height="23" Name="button1" Width="75" />

  给一个button按钮绑定Open命令,但是这个命令并没有被激活,所以该按钮是不可以点击的;

  需要添加一些代码

  <Window.CommandBindings>

    <CommandBinding Command="Open" Executed="BtnOpen"></CommandBinding>

  </Window.CommandBindings>

  用Window来接受这个命令,使用BtnOpen这个事件来进行处理;BtnOpen事件的后台代码是:

  private void BtnOpen(object sender, ExecutedRoutedEventArgs e)

  {

    MessageBox.Show("You Click " + button1.Content + " Command");

  }

二、因为例如textbox这类的控件内部实现了copy这类的处理事件,再加上ToolBar、Menu这个控件也实现了一些功能,所以有了另外两种绑定的方式;(所以也不用写自己写后

台事件处理代码)

  1、使用普通的Button按钮绑定copy命令

    <Button Command="Copy" CommandTarget="{Binding ElementName=txtDocument}" Content="Copy" Height="23" HorizontalAlignment="Left" Margin="6,19,0,0" Name="button2" VerticalAlignment="Top" Width="75" />
                <Button Command="Cut" CommandTarget="{Binding ElementName=txtDocument}" Content="Cut" Height="23" HorizontalAlignment="Left" Margin="6,66,0,0" Name="button3" VerticalAlignment="Top" Width="75" />
                <Button Command="Paste" CommandTarget="{Binding ElementName=txtDocument}" Content="Paste" Height="23" HorizontalAlignment="Left" Margin="6,114,0,0" Name="button4" VerticalAlignment="Top" Width="75" />
                <TextBox Height="23" HorizontalAlignment="Left" Margin="122,20,0,0" Name="txtDocument" VerticalAlignment="Top" Width="120" />

  其中Command="Copy"是绑定命令,该按钮使命令源;

  CommandTarget="{Binding ElementName=txtDocument}"是指定命令目标;

  2、使用ToolBar控件绑定copy命令

  <ToolBar Height="26" HorizontalAlignment="Left" Margin="6,20,0,0" Name="toolBar1" VerticalAlignment="Top" Width="200" >
                    <Button Command="Copy" Content="{Binding RelativeSource={RelativeSource Self},Path=Command.Text}"></Button>
                    <Button Command="Cut" Content="{Binding RelativeSource={RelativeSource Self},Path=Command.Text}"></Button>
                    <Button Command="Paste" Content="{Binding RelativeSource={RelativeSource Self},Path=Command.Text}"></Button>
      </ToolBar>
      <TextBox Height="23" HorizontalAlignment="Left" Margin="31,82,0,0" Name="txtDocumentSecond" VerticalAlignment="Top" Width="120" />

  其中Command="Copy"是绑定命令,该按钮使命令源;

  Content="{Binding RelativeSource={RelativeSource Self},Path=Command.Text}"是将command的命令的Text赋值给Button的Content上,此Text具有本地化的功能;

其中的命令绑定应该类似,希望能和大家分享更多的学习笔记和源代码

大家一起交流

原文地址:https://www.cnblogs.com/gengyuanchao/p/2714290.html