潜移默化学会WPF--Command(命令)学习(二) - AYUI框架 - 博客园

原文: 潜移默化学会WPF--Command(命令)学习(二) - AYUI框架 - 博客园

2. 基本功

   2.1 先看一段代码

前台页面上的
复制代码
 1 <Window x:Class="Commands.SimpleDocument"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 Title="SimpleDocument" Height="300" Width="300"
5 >
6 <Grid>
7 <Grid.RowDefinitions>
8 <RowDefinition Height="Auto"></RowDefinition>
9 <RowDefinition Height="Auto"></RowDefinition>
10 <RowDefinition></RowDefinition>
11 </Grid.RowDefinitions>
12 <Menu Grid.Row="0">
13 <MenuItem Header="File">
14 <MenuItem Command="New"></MenuItem>
15 <MenuItem Command="Open"></MenuItem>
16 <MenuItem Command="Save"></MenuItem>
17 <MenuItem Command="SaveAs"></MenuItem>
18 <Separator></Separator>
19 <MenuItem Command="Close"></MenuItem>
20 </MenuItem>
21 </Menu>
22
23 <ToolBarTray Grid.Row="1">
24 <ToolBar>
25 <Button Command="New">New</Button>
26 <Button Command="Open">Open</Button>
27 <Button Command="Save">Save</Button>
28 </ToolBar>
29 <ToolBar>
30 <Button Command="Cut">Cut</Button>
31 <Button Command="Copy">Copy</Button>
32 <Button Command="Paste">Paste</Button>
33
34 </ToolBar>
35 </ToolBarTray>
36 <TextBox Name="txt" Margin="5" Grid.Row="2"
37 TextWrapping="Wrap" AcceptsReturn="True"
38 TextChanged="txt_TextChanged"></TextBox>
39 </Grid>
40 </Window>
复制代码

当命令是系统定义的Command后面的值中的 ApplicationCommands可以不写,直接写后面的值

前台能定义的,后台也能用代码定义好,这点要坚信

例如

 CommandBinding binding= new CommandBinding(ApplicationCommands.Save);

 binding.Executed += SaveCommand_Executed;

 binding.CanExecute += SaveCommand_CanExecute;
 this.CommandBindings.Add(binding);

 

类似等同于

 <Window.Resources>

<CommandBinding x:Key="binding" Command="ApplicationCommands.Save"
Executed="SaveCommand" CanExecute="SaveCommand_CanExecute" />

 </Window.Resources>

备注:如果不写Key,所有的调了save命令的对象都执行 Executed定义好的方法, CanExecute表示是否执行 Executed定义好的方法,例如在后台代码中 SaveCommand_CanExecute这个方法中写 e.CanExecute=true,表示 SaveCommand这个方法,当触发时可以执行,设false就不可以执行了,这点类似于 控件的IsEnabled属性,能用不能用,IsEnabled=false;此按钮单击也没效果了,e.CanExecute=false跟那个一个道理

然后这样调用

<TextBox Margin="5" Grid.Row="2"  TextWrapping="Wrap" AcceptsReturn="True" >

<TextBox.CommandBindings>
    <StaticResource ResourceKey="binding"></StaticResource>
</TextBox.CommandBindings>
</TextBox>

备注:AcceptsReturn=true表示此文本框可以 按回车键换行

 

下一章讲 自定义命令

分类: WPF
0
0
« 上一篇: 潜移默化学会WPF--Command(命令)学习(一)
» 下一篇: 潜移默化学会WPF--Command(命令)学习(三)
posted on 2019-03-01 22:05 NET未来之路 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/10459040.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值