Dot-Net

將 KeyEventArgs 從 WPF 中的視圖傳遞給 ViewModel (MVVM)

  • October 13, 2013

我有一個文本框,我試圖將 KeyEventArgs 從視圖傳遞到視圖模型。但我不知道如何實現它。基本上我需要的是,如果輸入了一些特殊字元,那麼如果輸入了普通文本(如 A、B、C..etc),則呼叫一些函式,然後呼叫一些其他函式,如果按下 Enter 鍵,則呼叫一些函式將呼叫其他函式。如何在 MVVM 中執行此操作。我在 VS 2012 中使用 WPF。

有很多方法。讓我一一解釋。1.如果您只有一些選定的鍵並且在按下這些選定的鍵時只有一些功能要實現,那麼最好的方法是以下

<TextBox x:Name="tboxCouponSearch" Text="{Binding SearchMatchHomeorVisitor,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource EfesInputTextbox}" Width="170" Height="26" AcceptsReturn="False" TabIndex="40" TextWrapping="NoWrap" KeyDown="tboxCouponSearch_KeyDown_1">
                               <TextBox.InputBindings>
                                   <KeyBinding Key="Enter" Command="{Binding SearchTextboxEnterKeyCommand}"/>
                                   <KeyBinding Key="Left" Command="{Binding LeftRightUpDownARROWkeyPressed}"  />
                                   <KeyBinding Key="Down" Command="{Binding LeftRightUpDownARROWkeyPressed}"  />
                                   <KeyBinding Key="Up" Command="{Binding LeftRightUpDownARROWkeyPressed}"  />
                                   <KeyBinding Key="Right" Command="{Binding LeftRightUpDownARROWkeyPressed}"  />
                               </TextBox.InputBindings>                                                               
                           </TextBox>

在上面的範例中,您可以在點擊那些特定鍵時看到這些命令將被執行並傳遞給視圖模型。然後像往常一樣在視圖模型中呼叫這些函式。

2.如果要跟踪所有鍵而不考慮按下哪個鍵,那麼最好使用

<TextBox x:Name="tboxCouponSearch" Text="{Binding SearchMatchHomeorVisitor,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Style="{StaticResource EfesInputTextbox}" Width="170" Height="26" AcceptsReturn="False" TabIndex="40" TextWrapping="NoWrap" KeyDown="tboxCouponSearch_KeyDown_1">                                
                               <i:Interaction.Triggers>
                                   <i:EventTrigger EventName="KeyUp">
                                       <i:InvokeCommandAction Command="{Binding SearchTextBoxCommand}" CommandParameter="{Binding Path=Text, RelativeSource={RelativeSource AncestorType={x:Type TextBox}}}"/>
                                   </i:EventTrigger>                                       
                               </i:Interaction.Triggers>                                
                           </TextBox>

現在這將在所有按鍵按下或按鍵向上事件時觸發..以及您想呼叫的任何函式都可以在視圖模型中呼叫。(為此包括項目調試文件夾中的interaction.dll和intereactivity.dll(您將獲得在 C 驅動器的程序文件中安裝 Blend 時的那些 dll。

3.如果是這樣的情況,例如在某個特定鍵上要呼叫函式或在其他鍵的按鍵上要呼叫其他函式。那麼你必須在後面的程式碼中做。

private void Window_KeyUp_1(object sender, KeyEventArgs e)
       {
           try
           {
               mainWindowViewModel.KeyPressed = e.Key;

通過這種方式,您可以擷取 keyeventargs .. mainWindowViewModel 是 viewModel 的一個實例。現在在視圖模型中你這樣做

private Key _keyPressed ;
       public Key KeyPressed
       {
           get
           {
               return _keyPressed;
           }
           set
           {
               _keyPressed = value;
               OnPropertyChanged("KeyPressed");
           }
       }

現在在 Viewmodel 中以以下方式實現此屬性

bool CanSearchTextBox
       {
           get
           {
               if (KeyPressed != Key.Up && KeyPressed != Key.Down && KeyPressed != Key.Left && KeyPressed != Key.Right && MatchSearchList!=null)
                   return true;
               else
                   return false;
           }
       }

引用自:https://stackoverflow.com/questions/19345192