控件绑定Command

it2022-05-09  41

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

< 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


最新回复(0)