【发布时间】:2016-07-27 21:03:55
【问题描述】:
我在我的 wpf 应用程序项目中使用 mvvm light。为了收听事件,我使用了来自 MVVM Light 库的EventToCommand。
控制代码如下:
<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 不保留下一个事件处理程序的状态?
【问题讨论】: