首页 技术 正文
技术 2022年11月20日
0 收藏 805 点赞 2,074 浏览 2108 个字

What

命令包含以下部分:

命令:一个实现了ICommand接口的类,RoutedCommand是WPF里最常用的命令类,其它命令类大多派生自RoutedCommand

命令源:触发命令的对象,如button,menu等

命令目标:默认为命令源对象,显示指定CommandTarget=XXX之后,实际执行命令的对象为XXX,可通过执行函数的e.Source进行确认

命令关联:把命令源和命令以及命令的逻辑函数关联起来

why

命令可以实现一处定义,处处使用的好处。不同的命令源只要绑定同一个命令就会执行该命令绑定的执行函数。

解除前后端耦合,通过XAML绑定命令,后台代码类中可不再引用窗体控件

how

1、定义命令

        public static ICommand AddCommand = new RoutedCommand();

  

2、定义命令执行函数

        public void Add(object sender, ExecutedRoutedEvente e)
{
MessageBox.Show(e.Parameter.ToString()); //标记为已处理,不再向上传递
e.Handled = true;
}
public void CanAdd(object sender, CanExecuteRoutedEvente e)
{
if (string.IsNullOrEmpty(e.Parameter.ToString()))
e.CanExecute = false;
else
e.CanExecute = true; e.Handled = true;
}

3、关联绑定命令和执行函数,并添加到控件的关联绑定集合

       this.CommandBindings.Add(new CommandBinding(AddCommand, Add, CanAdd));

  

4、绑定命令源

       <Button Content="clickme"
Command="{x:Static local:TestWindow.AddCommand}"
CommandParameter="{Binding Text ,ElementName=txtMsg}" />

  

完整代码:

XAML

<Window x:Class="WpfTest.TestWindow"
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:WpfTest"
mc:Ignorable="d"
Title="TestWindow" Height="" Width=""> <StackPanel>
<TextBox x:Name="txtMsg" Text="" />
<!--第四步:把命令绑定到命令源-->
<Button Content="clickme"
Command="{x:Static local:TestWindow.AddCommand}"
CommandParameter="{Binding Text ,ElementName=txtMsg}" />
</StackPanel>
</Window>

XAML.CS

using System.Windows;
using System.Windows.Input;namespace WpfTest
{
/// <summary>
/// TestWindow.xaml 的交互逻辑
/// </summary>
public partial class TestWindow : Window
{
//第一步:定义命令
public static ICommand AddCommand = new RoutedCommand(); //第二步:定义执行函数
public void Add(object sender, ExecutedRoutedEvente e)
{
MessageBox.Show(e.Parameter.ToString()); //标记为已处理,不再向上传递
e.Handled = true;
}
public void CanAdd(object sender, CanExecuteRoutedEvente e)
{
if (string.IsNullOrEmpty(e.Parameter.ToString()))
e.CanExecute = false;
else
e.CanExecute = true; e.Handled = true;
} public TestWindow()
{
InitializeComponent();
//第三步:关联命令和执行函数,并添加到窗体的命令绑定集
this.CommandBindings.Add(new CommandBinding(AddCommand, Add, CanAdd)); }
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,565
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905