Is it possible to fire a command to notify the window is loaded. Also, I'm not using any MVVM frameworks (Frameworks in the sense, Caliburn, Onxy, MVVM Toolkit etc.,)
How to fire a Command when a window is loaded in wpf
Asked Answered
Almost all events can't fire a command in the ViewModel. You can just accept the fact and write 1 line of code in the CodeBehind or implement some obscure pattern that after several intricate lines of code do the same. #dontbeapurist –
Battleax
I disagree with @EduardoMolteni. If the event is related to work with data, then you will need to work with the VM at code behind, which can be easly avoided using behaviours in WPF. –
Chatelaine
To avoid code behind on your View, use the Interactivity library (System.Windows.Interactivity dll which you can download for free from Microsoft - also comes with Expression Blend).
Then you can create a behavior that executes a command. This way the Trigger calls the Behavior which calls the Command.
<ia:Interaction.Triggers>
<ia:EventTrigger EventName="Loaded">
<custombehaviors:CommandAction Command="{Binding ShowMessage}" Parameter="I am loaded"/>
</ia:EventTrigger>
</ia:Interaction.Triggers>
CommandAction (also uses System.Windows.Interactivity) can look like:
public class CommandAction : TriggerAction<UIElement>
{
public static DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(CommandAction), null);
public ICommand Command
{
get
{
return (ICommand)GetValue(CommandProperty);
}
set
{
SetValue(CommandProperty, value);
}
}
public static DependencyProperty ParameterProperty = DependencyProperty.Register("Parameter", typeof(object), typeof(CommandAction), null);
public object Parameter
{
get
{
return GetValue(ParameterProperty);
}
set
{
SetValue(ParameterProperty, value);
}
}
protected override void Invoke(object parameter)
{
Command.Execute(Parameter);
}
}
I just realized this code is for Silverlight. If you can find the equivalent trigger/behaviors in WPF, I would think the same principals should work though. –
Lake
Instead of CommandAction, there seems to be an existing action for this now, InvokeCommandAction –
Bohn
I've found this is much easier now that you don't have to load in the library from expression: https://mcmap.net/q/964280/-how-to-fire-a-command-when-a-window-is-loaded-in-wpf –
Reformer
private void Window_Loaded(object sender, RoutedEventArgs e)
{
ApplicationCommands.New.Execute(null, targetElement);
// or this.CommandBindings[0].Command.Execute(null);
}
and xaml
Loaded="Window_Loaded"
I forgot to mention I'm using MVVM. When, a window is loaded. It should fire a command, that I'll be able to listen in my ViewModel class. –
Bombast
MVVM is not a religion. You can add a line of code in the CodeBehind and the world will still be spinning. –
Battleax
Are you sure! are you sure it will continue to spin? :) –
Cretan
Just as long as that line of code is only a responsibility of the view. :) –
Mandle
Haha. Your comment is epic @EduardoMolteni . Thanks. –
Blowpipe
A more generic way using behaviors is proposed at AttachedCommandBehavior V2 aka ACB and it even supports multiple event-to-command bindings,
Here is a very basic example of use:
<Window x:Class="Example.YourWindow"
xmlns:local="clr-namespace:AttachedCommandBehavior;assembly=AttachedCommandBehavior"
local:CommandBehavior.Event="Loaded"
local:CommandBehavior.Command="{Binding DoSomethingWhenWindowIsLoaded}"
local:CommandBehavior.CommandParameter="Some information"
/>
This is much easier to do now. Simply include the following namespace:
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
And leverage it like this:
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<i:InvokeCommandAction Command="{Binding CommandInViewModel}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
This doesn't work for the Loaded event because the trigger is only wired up after the Loaded event has already completed. I've achieved the desired result using the ContentRendered event. It's not clear, but it appears that this event only fires the first time the window renders. But that may not be true if, for instance, the controls within the window can change, such as swapping out user controls when a view model is changed (as oppose to manipulating controls already present, but hidden/collapsed). –
Metcalfe
© 2022 - 2024 — McMap. All rights reserved.