WPF中添加一個文本輸入框,按Enter回車,執行綁定的Command


在WPF+WMMV模式中使用鍵盤和鼠標事件的綁定代碼如下:

<TextBox x:Name="SearchBox" Text="{Binding SearchText}" Width="246" Height="24" HorizontalAlignment="Right" PreviewKeyDown="SearchBox_OnKeyDown">
             <TextBox.InputBindings>
                   <KeyBinding Command="{Binding KeyEventCommand}" Key="Enter"/>//綁定鍵盤輸入事件

            <dxg:GridControl.InputBindings>

                <MouseBinding Command="{Binding ProductDoubleClickCommand}" CommandParameter="{Binding ElementName=ProductCtrl,Path=CurrentItem}"

                                                                 MouseAction="LeftDoubleClick"/>//綁定鼠標事件

           </dxg:GridControl.InputBindings>

             </TextBox.InputBindings>
 </TextBox>

上面需要注意的是:搜索文本框的輸入文本在按Enter后雖然會觸發事件,但是獲取不到搜索文本框的輸入文本值,因此需要是搜索文本框的輸入文本在按Enter后失去焦點,

多以添加PreviewKeyDown="SearchBox_OnKeyDown,

 private void SearchBox_OnKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                SearchBox.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
            }
        }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM