I have the following Xaml where I'm using a behavior to activate my Login window:
<Window ...
xmlns:behaviors="clr-namespace:"..."
xmlns:interactivity="http://schemas.microsoft.com/xaml/behaviors"
.
.>
<interactivity:Interaction.Behaviors>
<behaviors:ActivateBehavior Activated="{Binding Activated, Mode=TwoWay}"/>
</interactivity:Interaction.Behaviors>
.
.
</Window>
together with the corresponding Behavior that reacts on the OnActivated event for a Window:
public class ActivateBehavior : Behavior<Window>
{
.
. (here goes some other code like the DP Activated)
.
protected override void OnAttached()
{
AssociatedObject.Activated += OnActivated;
AssociatedObject.Deactivated += OnDeactivated;
}
protected override void OnDetaching()
{
AssociatedObject.Activated -= OnActivated;
AssociatedObject.Deactivated -= OnDeactivated;
}
void OnActivated(object sender, EventArgs eventArgs)
{
_isActivated = true;
Activated = true;
if (string.IsNullOrEmpty(App.UserId))
{
LoginView loginView = new LoginView();
loginView.ShowDialog();
}
}
void OnDeactivated(object sender, EventArgs eventArgs)
{
_isActivated = false;
Activated = false;
}
}
You can implement this in code-behind by using the following code:
protected override void OnActivated(EventArgs e)
{
base.OnActivated(e);
if (string.IsNullOrEmpty(App.UserId))
{
LoginView loginView = new LoginView();
loginView.ShowDialog();
}
}
but since I'm only working with MVVM this is not an option. Now, my qustion is why this cannot be implemented with an EventTrigger instead, i.e., using the following code in my xaml:
<Window ...
xmlns:behaviors="clr-namespace:"..."
xmlns:interactivity="http://schemas.microsoft.com/xaml/behaviors"
.
.>
<interactivity:Interaction.Triggers>
<interactivity:EventTrigger EventName="Activated">
<interactivity:InvokeCommandAction Command="{Binding OnActivatedCommand}" />
</interactivity:EventTrigger>
</interactivity:Interaction.Triggers>
.
.
</Window>
with the following command in my NotesViewModel.cs:
public RelayCommand OnActivatedCommand { get; set; }
and in my NotesViewModel constructor:
OnActivatedCommand = new RelayCommand(o =>
{
if (string.IsNullOrEmpty(App.UserId))
{
LoginView loginView = new LoginView();
loginView.ShowDialog();
}
});
With this implementation the command is never hit which means the EventTrigger "Activated" is never hit.
I know there's another discussion whether you should reference another view in the ViewModel but that's not what I'm out for here, I just want to know why I can't use Interaction.Triggers and EventTrigger to fire the event Activated instead of using Interaction.Behaviors (which nevertheless should be the MVVM Purist way I would say)?
Thanks.