#342 – 将Button 和Command绑定(Binding a Button to a Command)
来源:互联网 发布:网络公司财务 编辑:程序博客网 时间:2024/06/13 12:02
在WPF中,按钮点击操作执行后要执行的代码通常使用命令(command)的方式来实现。命令(command)表示一个即将执行的操作,并为这个操作指定了一个特定的方法。按钮可以通过设置其Command 属性为其绑定关联的命令。
下面是一个例子:
<Button Content="Open" Command="ApplicationCommands.Open" />
ApplicationCommands.Open 是一个预定义好的命令,你通过在父窗口中添加一个新的CommandBinding 实例到CommandBinding 集合中绑定命令。
public MainWindow(){ this.InitializeComponent(); CommandBindings.Add(new CommandBinding(ApplicationCommands.Open, Open_Executed, Open_CanExecute));} public void Open_Executed(object sender, ExecutedRoutedEventArgs e){ // Open file here} public void Open_CanExecute(object sender, CanExecuteRoutedEventArgs e){ e.CanExecute = true; // Can we open file?}
原文地址:https://wpf.2000things.com/2011/07/14/342-binding-a-button-to-a-command/
**************************************************译者注**********************************************
在上面的例子中ApplicationCommands.Open 是一个ICommand接口的实例,它表示一个命令。Open_Executed表示与该命令执行的时候要执行的方法。Open_CanExecute表示一个检查方法,以确定是否在命令目标上执行该命令。
同一个命令可以和多个控件绑定,例如常见的窗口菜单可以绑定“打开文件”命令,同时也可以和一个打开的按钮绑定。
0 0
- #342 – 将Button 和Command绑定(Binding a Button to a Command)
- #358 – 将RadioButton和枚举绑定(Binding a RadioButton to an Enumerated Type)
- #347 – 通过Command设置按钮的内容(How to Set Content Property for Button Based on Command)
- Command Button -- New Request
- mfc 对话框 Command button
- a 和 button
- WPF中DataGrid控件内Button的Command和CommandParameter的绑定
- #375 – 将Tooltip 于父控件的某个属性绑定(Binding Something in a Tooltip to a Property on the Parent Control)
- How to add a button to PreferenceScreen
- How to Execute a Command in C# ?
- Write a program to pratice ATA command
- a while-switch to read input command
- Passing Command Line Arguments to a Module
- How to Execute a Command in C# ?
- button cannot resolved to a type
- How to programmatically click a WPF button
- How To: Assign a Macro to a Button or Shape
- Submitting a Command Buffer
- yii1.0 细小累计
- 计划任务,但可能不能运行,因为无法设置帐户信息,指定的错误是:0X80041315
- java_mail收发邮件
- 在浏览器中禁止拖动页面的元素或者拖动图片打开新的窗口
- web配置p6spy
- #342 – 将Button 和Command绑定(Binding a Button to a Command)
- ftp开发机命令使用
- 小白初次使用云主机云服务器(腾讯云)
- SpringMVC跨域乱码问题
- Day46、异常、I/O流
- 警告背后的use after free
- SpringMvc 遇到的坑,返回中文乱码以及Ajax跨域
- Java实现图片的压缩(包括文件上传的图片压缩)
- CentOS升级Python 2.6到2.7