2016-07-27 24 views
-1

wpfアプリケーションプロジェクトでmvvm lightを使用しています。イベントを聞くために、MVVM LightライブラリのEventToCommandを使用しています。MVVM Light e.Handledは状態を保持しません

制御コードは以下のようになります。

<TextBox x:Name="Scannerport" 
      Grid.Row="1" 
      Grid.Column="1" 
      Margin="15,10,40,10" 
      MinWidth="100" 
      FontSize="40" 
      MaxLength="2" 
      PreviewTextInput="Scaleport_OnPreviewTextInput" 
      VerticalContentAlignment="Center" 
      HorizontalContentAlignment="Center" 
      Text="{Binding ScannerPort, UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True, NotifyOnValidationError=True}"> 
     <i:Interaction.Triggers> 
     <i:EventTrigger EventName="TextChanged"> 
      <cmd:EventToCommand Command="{Binding OnTextChanged}" 
           PassEventArgsToCommand="True" /> 
     </i:EventTrigger> 


     <rt:RoutedEventTrigger RoutedEvent="{x:Static Validation.ErrorEvent}"> 
      <cmd:EventToCommand Command="{Binding OnValidationError}" PassEventArgsToCommand="True" /> 
     </rt:RoutedEventTrigger> 

     </i:Interaction.Triggers> 
    </TextBox> 

とのViewModelでの実装コード:

private void _OnTextChanged(TextChangedEventArgs e) 
    { 
     Debug.WriteLine(e.Handled); 
     if (ScalePort != 0 && ScannerPort != 0) 
     { 
     Disable = true; 
     return; 
     } 

     Disable = false; 
    } 

    private void _OnValidationError(ValidationErrorEventArgs e) 
    { 
     if (e.Action == ValidationErrorEventAction.Added) 
     { 
     Disable = true; 
     e.Handled = true; 
     } 
    } 

あなたは第二の方法で見ることができるように、私はe.Handled = trueを設定し、その後にデバッグ中最初の方法e.Handledはまだfalseですか? なぜe.Handledは次のイベントハンドラの状態を保持しませんか?

enter image description here enter image description here

答えて

1

は、なぜあなたはそれを保存していなかったと思いますか?それはちょうど異なる出来事であり、お互いにコミュニケーションを取ることはありません。だからTextChangedイベントでe.Handled== trueを取得することはできません。

+0

したがって、 'e.Handled'を次のハンドラに渡しませんか? –

+0

@zero_codingはいそうではありません。検証とPropertyChangedイベントは直接接続されていません。 WPFでエラーをチェックし、オブジェクトをバインドしたオブジェクト(この例ではTextBox)に追加します。そして、propertychangedイベントはあなたにいくつかの変更があることを伝えます。 [Validation.Errors添付プロパティ](https://msdn.microsoft.com/en-us/library/system.windows.controls.validation.errors(v = vs.110).aspx)にチェックを入れてください。コントロールのエラーをチェックしてください。 – Shakra

+0

ありがとうございました。 –

1

e.Handled = trueとは異なる場合があります。

PreviewTextInputイベントを聞き、TextChangedイベントの再開を停止するには、e.Handled = trueを設定します。

関連する問題