控件绑定Command

情况一:普通按钮单击事件

< Button   Content ="Button1"  
         Command
=" {Binding CommandSave} "  
         IsEnabled
=" {Binding IsEnableSave,Mode=TwoWay} "  
         Margin
="10,0,0,0"   Width ="60"  Height ="23"
         VerticalContentAlignment
="Center"  HorizontalContentAlignment ="Center"    />

情况二:特殊事件处理(使用System.Windows.Interactivity处理)

引入命名空间:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

 

< TextBox  x:Name ="TextBox1"
         Grid.Column
="0"  Grid.Row ="0"
         
HorizontalAlignment ="Left"  VerticalAlignment ="Center"
         Margin
="0,0,0,0"  Width ="160"  Height ="23" >
          < i:Interaction.Triggers >
                  < i:EventTrigger  EventName ="TextChanged" >
                           < i:InvokeCommandAction  Command =" {Binding CommandPageIndexChanged} "  CommandParameter =" {Binding ElementName=ss,Path=Text} " />
                  </ i:EventTrigger >
         </ i:Interaction.Triggers >
  </ TextBox >

情况三:绑定键盘事件(求最优解决方案)

 方案一:只能借用CodeBehind来调用ViewModel的Command

< TextBox  x:Name ="TextBox1"
         Grid.Column
="0"  Grid.Row ="0"
         
KeyDown ="txtFFaraCode_KeyDown_1" />

 

void txtFFaraCode_KeyDown( object sender, KeyEventArgs e)
        {
             if (e.Key ==Key.Enter)
            {
                ViewModels.QuoteWinNewVM vm = (ViewModels.QuoteWinNewVM) this.DataContext;
                vm.CommandFaraCodeKeyDown.Execute();
            }
        }

 方案二:用KeyTrigger最好了,然后在ViewModel实现Command  此方法测试失败,求解决方案

  < TextBox  x:Name ="TextBox1" >
          < i:Interaction.Triggers >
                  < input:KeyTrigger  Key ="Enter"  FiredOn ="KeyUp" >
                         < i:InvokeCommandAction  Command =" {Binding CommandFaraCodeKeyDown} "
                                                CommandParameter
=" {Binding ElementName=ss,Path=Text} " />
                 </ input:KeyTrigger >
          </ i:Interaction.Triggers >
  </ TextBox >

 

转载于:https://www.cnblogs.com/nnkook/archive/2012/03/30/2424735.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值