这是因为事件的使用显式地破坏了 MVVM 模式(我相信你知道)。然而,还有另一种解决方法——使用附加命令行为模式。更多信息在这里。
可从此处下载小而强大的附加命令框架的代码。
我希望这有帮助。
编辑。附加行为允许您在不破坏 MVVM 模式的情况下使用事件。用途就像
<Border Background="Yellow" Width="350" Margin="0,0,10,0" Height="35" CornerRadius="2" x:Name="test">
<local:CommandBehaviorCollection.Behaviors>
<local:BehaviorBinding Event="MouseLeftButtonDown"
Action="{Binding DoSomething}"
CommandParameter="An Action on MouseLeftButtonDown"/>
<local:BehaviorBinding Event="MouseRightButtonDown"
Command="{Binding SomeCommand}"
CommandParameter="A Command on MouseRightButtonDown"/>
</local:CommandBehaviorCollection.Behaviors>
<TextBlock Text="MouseDown on this border to execute the command"/>
</Border>