一、实例概述
下面通过一个例子来说明如何使用ApplicationCommands的Copy命令。在该例中,通过点击按钮实现把一个文本框的内容拷贝到另外一个文本框中。
虽然说微软提供了WPF命令库,但是处理函数还是要我们自己去写的。一般我们在窗体中编写命令绑定,这样窗体中的所有元素都可以通过命令调用窗体命令绑定的处理函数。
二、使用命令库中的命令有两种方式:XAML代码和C#后台代码
①XAML代码方式
xaml前台代码:
<Window x:Class="CommandTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CommandTest"
mc:Ignorable="d"
Title="MainWindow" Height="180" Width="300">
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.Copy" CanExecute="Copy_CanExecute" Executed="Copy_Executed"></CommandBinding>
</Window.CommandBindings>
<StackPanel>
<TextBox x:Name="txt1" Width="200" Margin="10,20,10,10"></TextBox>
<TextBox x:Name="txt2" Width="200" Margin="10"></TextBox>
<Button x:Name="btn1" Width="100" Margin="10" Command="ApplicationCommands.Copy">复制</Button>
</StackPanel>
</Window>
对应的C#后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 CommandTest
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Copy_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = !string.IsNullOrEmpty(txt1.Text);
}
private void Copy_Executed(object sender, ExecutedRoutedEventArgs e)
{
txt2.Text = txt1.Text;
}
}
}
实现效果:
当第一个文本框为空时,命令不可执行,复制按钮不可用
当第一个文本框不为空时,命令可执行,复制按钮可用
点击复制按钮实现将第一个文本框内容复制到第二个文本框中
②C#后台代码方式
xaml前台代码:
<Window x:Class="CommandTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CommandTest"
mc:Ignorable="d"
Title="MainWindow" Height="180" Width="300">
<StackPanel>
<TextBox x:Name="txt1" Width="200" Margin="10,20,10,10"></TextBox>
<TextBox x:Name="txt2" Width="200" Margin="10"></TextBox>
<Button x:Name="btn1" Width="100" Margin="10">复制</Button>
</StackPanel>
</Window>
对应的C#后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 CommandTest
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
InitializeCommand();
btn1.Command = ApplicationCommands.Copy;
}
private void InitializeCommand()
{
CommandBinding cb = new CommandBinding();
cb.Command = ApplicationCommands.Copy;
cb.CanExecute += Copy_CanExecute;
cb.Executed += Copy_Executed;
this.CommandBindings.Add(cb);
}
private void Copy_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = !string.IsNullOrEmpty(txt1.Text);
}
private void Copy_Executed(object sender, ExecutedRoutedEventArgs e)
{
txt2.Text = txt1.Text;
}
}
}
执行效果同上
当两个命令源调用同一个命令时,在处理函数中可通过e.Parameter属性来区分是哪个命令源发出的命令。
xaml前台代码:
<Window x:Class="CommandTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:CommandTest"
mc:Ignorable="d"
Title="MainWindow" Height="230" Width="300">
<StackPanel>
<TextBox x:Name="txt1" Width="200" Margin="10,20,10,10"></TextBox>
<TextBox x:Name="txt2" Width="200" Margin="10"></TextBox>
<Button x:Name="btn1" Width="100" Margin="10">复制1</Button>
<Button x:Name="btn2" Width="100" Margin="10">复制2</Button>
</StackPanel>
</Window>
对应的C#后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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 CommandTest
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
InitializeCommand();
btn1.Command = ApplicationCommands.Copy;
btn1.CommandParameter = "btn1";
btn2.Command = ApplicationCommands.Copy;
btn2.CommandParameter = "btn2";
}
private void InitializeCommand()
{
CommandBinding cb = new CommandBinding();
cb.Command = ApplicationCommands.Copy;
cb.CanExecute += Copy_CanExecute;
cb.Executed += Copy_Executed;
this.CommandBindings.Add(cb);
}
private void Copy_CanExecute(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = !string.IsNullOrEmpty(txt1.Text);
}
private void Copy_Executed(object sender, ExecutedRoutedEventArgs e)
{
if ((string)e.Parameter == "btn1")
{
txt2.Text = "复制1:" + txt1.Text;
}
else if((string)e.Parameter == "btn2")
{
txt2.Text = "复制2:" + txt1.Text;
}
}
}
}
效果演示: