EventToCommand for DataWindow loaded event not firing binded command

Topics: Questions
Jul 18, 2012 at 3:18 PM
Any ideas why this is not working? I was thinking to something related to initialization sequence. Did you already know about this behaviour?
<i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <catel:EventToCommand Command="{Binding WindowLoadedCommand}" DisableAssociatedObjectOnCannotExecute="False" PassEventArgsToCommand="False" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
Coordinator
Jul 18, 2012 at 4:20 PM

Probably the trigger is attached to the window *after* being loaded. Can you add a breakpoint to this event in code-behind as well, and see what is fired first?

Jul 19, 2012 at 4:55 PM
Edited Jul 21, 2012 at 1:36 PM

After some investigation, I came up with this:

 

        private void DataWindow_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            var windowTriggers = Interaction.GetTriggers(this);
            EventToCommand eventToCommand = (EventToCommand) windowTriggers[0].Actions[0];
            //eventToCommand.Command  <-- The trigger is already in the collection, but Command is NULL
            eventToCommand.Invoke(); //nothing happens
        }

        private void DataWindow_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            var windowTriggers = Interaction.GetTriggers(this);
            EventToCommand eventToCommand = (EventToCommand)windowTriggers[0].Actions[0];
            //eventToCommand.Command  <-- Here the command has been assigned
            eventToCommand.Invoke(); //Command handler gets executed
        }

It is clear to me that there is a moment after the loaded event in which the eventToCommand mechanism gets completed but, for now, I can't find when it happens.

Jul 19, 2012 at 5:15 PM

Ok, after further investigation, I think the best candidate is "ContentRendered". Now I want to be sure that this event gets fired only one time when the form shows up (official documentation is not so clear about it).

Coordinator
Jul 19, 2012 at 7:05 PM
Edited Jul 19, 2012 at 7:06 PM

I must have been somewhere else with my mind when I answered this question. Why don't you use the Initialize() method on the view model? It is built exactly for this scenario (called when the view is loaded).

Jul 21, 2012 at 1:35 PM

That was exactly what I needed. Thanks as always!

Jan 3, 2014 at 5:52 PM
Edited Jan 3, 2014 at 6:10 PM
I have the same issue. I however do want some command to run every time the view becomes visible. I can't seem to get any EventToCommand to work. I have regular button, combobox, etc... commands on the same view working with no issues. However the following:
<i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseDown">
        <catel:EventToCommand Command="{Binding LoadBankNames}"/>
    </i:EventTrigger>
</i:Interaction.Triggers>
Does not work no matter what event I use in "EventName" (using pre-release 3.8.1312281745-beta).

When adding the trigger to a Panel or other control, the EventToCommand works. When added to the catel:UserControl itself, it seems to stop working. Is this intended behaviour?
Coordinator
Jan 3, 2014 at 6:58 PM