Note that there are some explanatory texts on larger screens.

plurals
  1. POEventToCommand fails for the initial GotFocus event
    text
    copied!<p>I begin to apply the Mvvm design pattern in my current project and the framework I used is Mvvm Light toolkit. Now I encoutered a problem when using the EventToCommand to handle "GotFocus" event. The xaml file is something like:</p> <pre><code>&lt;TextBox x:Name="TextBox1"&gt; &lt;i:Interaction.Triggers&gt; &lt;i:EventTrigger EventName="GotFocus"&gt; &lt;cmd:EventToCommand Command="{Binding TestCommand}"/&gt; &lt;/i:EventTrigger&gt; &lt;/i:Interaction.Triggers&gt; &lt;/TextBox&gt; </code></pre> <p>I want to execute the TestCommand in the view model whenever the "GotFocus" is fired. But the problem is that the "TestCommand" is not executed for the initial "GotFocus"(ie. when the window is loaded). I have debugged and found that the "GotFocus" event was actually fired but the Trigger was not invoked for unknown reason. Then I set the focus in the "Window.Loaded" event handler, it still failed.</p> <pre><code>protected void WindowLoaded(object sender, RoutedEventArgs e) { FocusManager.SetFocusedElement(this, TextBox1); // The focus is moved to TextBox1 but the associated command is not executed. } </code></pre> <p>But If I set the focus in the "Window.Activated" event handler, it is OK.</p> <pre><code> protected void WindowActivated(object sender, EventArgs e) { FocusManager.SetFocusedElement(this, TextBox1); // The focus is moved to TextBox1 and the command is executed. } </code></pre> <p>I am very confused about what happened. Could anyone explain it in detail?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload