【问题标题】:TextEdit_KeyDown Event binding to a commandTextEdit_KeyDown 事件绑定到命令
【发布时间】:2014-12-02 19:42:12
【问题描述】:

我有一个 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 事件绑定到命令,以便获得相同的结果。

【问题讨论】:

    标签: wpf mvvm binding devexpress mvvm-light


    【解决方案1】:

    看看DevExpress MVVM Framework 中的EventToCommand 行为:

    查看:

    <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);
        }
    }
    

    【讨论】:

      【解决方案2】:
       if (e.KeyCode == Keys.Enter)
          {
              //Do your stuff
      
          }
      

      e 是 EventArg,在调用时总是传输,所以你必须查看变​​量中传输的内容

      【讨论】:

        猜你喜欢
        • 2010-10-30
        • 1970-01-01
        • 2012-06-11
        • 2014-11-06
        • 2018-01-19
        • 2011-11-03
        • 2014-11-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多