TextEdit_KeyDown绑定到命令的事件

时间:2014-10-08 08:02:47

标签: wpf mvvm binding devexpress mvvm-light

我有一个TextEdit和一个这样的按钮:

<dxe:TextEdit Text="{Binding SearchText}" Width="200" Height="25" VerticalAlignment="Center"  KeyDown="TextEdit_KeyDown" />
<Button Command="{Binding SearchCommand}" VerticalAlignment="Center" Margin="20,0,0,0" >

当用户单击该按钮时,SearchCommand会成功运行并返回结果。当用户按 Enter 时,我希望发生同样的事情。如何将TextEdit_KeyDown事件绑定到命令,以便得到相同的结果。

2 个答案:

答案 0 :(得分:2)

查看来自EventToCommandDevExpress MVVM Framework行为:

查看:

<UserControl ...
    DataContext="{dxmvvm:ViewModelSource Type=local:SearchViewModel}"> 
    //...
    <dxe:TextEdit Text="{Binding SearchText}" Width="200" Height="25" VerticalAlignment="Center"> 
        <dxmvvm:Interaction.Behaviors> 
            <dxmvvm:EventToCommand EventName="KeyDown" 
                Command="{Binding SearchByKeyCommand}" 
                PassEventArgsToCommand="True"
            > 
        </dxmvvm:Interaction.Behaviors> 
    </dxe:TextEdit>
    <Button Command="{Binding SearchCommand}" VerticalAlignment="Center" Margin="20,0,0,0" >
    //...

<强>视图模型:

[POCOViewModel]
public class SearchViewModel {
    public virtual SearchText { 
       get ; 
       set; 
    }
    public void Search() {
        //...  
    }
    public void SearchByKey(KeyEventArgs) {
        Search();  
    }
    public bool CanSearchByKey(KeyEventArgs args) {
        return (args.KeyCode == Keys.Enter) && !string.IsNullOrEmpty(SearchText);
    }
}

答案 1 :(得分:0)

 if (e.KeyCode == Keys.Enter)
    {
        //Do your stuff

    }

e是EventArg,它在调用时始终传输,因此您必须查看变​​量中传输的内容