Yes it is bad practice, because you are directly referencing the ViewModel
from the View
which implies a dependency between the View
and the ViewModel
and thus tight coupling.
The pattern specifically calls for the View
NOT to be dependent on a specific ViewModel
instance or type. The idea here being decoupled Views
and ViewModels
for polymorphic Views
(the idea that you could show the same data multiple ways). In order for your View
to be reusable, it would need to not have a concrete dependency on a specific ViewModel
type.
A better way to handle this would be to use Commands
as Kamel says. However, not all events/interactions/controls necessarily allow Commands
to be invoked. However, you can attach Commands
to specific events using the System.Windows.Interactivity.dll
assembly which comes with the Blend SDK
.
This allows you to create XAML that looks like the following:
<i:Interaction.Triggers>
<i:EventTrigger EventName="LostFocus">
<i:InvokeCommandAction
Command="{Binding UpdateTextBoxBindingOnEnterCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type TextBox}}}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
..which allows the same functionality as an event handler to invoke a Command
, but in a decoupled MVVM-friendly fashion.