Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>EDIT 2: From looking at the source code it seems that internally it works like that: </p> <ol> <li>The <code>UIElement</code> calls <code>CommandManager.TranslateInput()</code> in reaction to user input (mouse or keyboard).</li> <li>The <code>CommandManager</code> then goes through <code>CommandBindings</code> on different levels looking for a command associated with the input.</li> <li>When the command is found its <code>CanExecute()</code> method is called and if it returns <code>true</code> the <code>Executed()</code> is called.</li> <li>In case of <code>RoutedCommand</code> each of the methods does essencially the same thing - it raises a pair of attached events <code>CommandManager.PreviewCanExecuteEvent</code> and <code>CommandManager.CanExecuteEvent</code> (or <code>PreviewExecutedEvent</code> and <code>ExecutedEvent</code>) on the <code>UIElement</code> that initiated the process. That concludes the first phase.</li> <li>Now the <code>UIElement</code> has class handlers registered for those four events and these handlers simply call <code>CommandManager.OnCanExecute()</code> and <code>CommandManager.CanExecute()</code> (for both preview and actual events).</li> <li>It is only here in <code>CommandManager.OnCanExecute()</code> and <code>CommandManager.OnExecute()</code> methods where the handlers registered with <code>CommandBinding</code> are invoked. If there are none found the <code>CommandManager</code> transfers the event up to the <code>UIElement</code>'s parent, and the new cycle begins until the command is handled or the root of the visual tree is reached.</li> </ol> </blockquote> <p>If you look at the CommandBinding class source code there is OnExecuted() method that is responsible for calling the handlers you register for PreviewExecuted and Executed events through CommandBinding. There is that bit there:</p> <pre><code>PreviewExecuted(sender, e); e.Handled = true; </code></pre> <p>this sets the event as handled right after your PreviewExecuted handler returns and so the Executed is not called.</p> <blockquote> <p>EDIT 1: Looking at CanExecute &amp; PreviewCanExecute events there is a key difference:</p> <pre><code> PreviewCanExecute(sender, e); if (e.CanExecute) { e.Handled = true; } </code></pre> <p>setting Handled to true is conditional here and so it is the programmer who decides whether or not to proceed with CanExecute. Simply do not set the CanExecuteRoutedEventArgs's CanExecute to true in your PreviewCanExecute handler and the CanExecute handler will be called. </p> <p>As to <code>ContinueRouting</code> property of Preview event - when set to false it prevents the Preview event from further routing, but it does not affect the following main event in any way.</p> </blockquote> <p>Note, that it only works this way when handlers are registered through CommandBinding.</p> <p>If you still want to have both PreviewExecuted and Executed to run you have two options: </p> <ol> <li>You can can call <code>Execute()</code> method of the routed command from within PreviewExecuted handler. Just thinking about it - you might run into sync issues as you're calling Executed handler before the PreviewExecuted is finished. To me this doesn't look like a good way to go.</li> <li>You can register PreviewExecuted handler separately through <code>CommandManager.AddPreviewExecutedHandler()</code> static method. This will be called directly from UIElement class and will not involve CommandBinding. <code>EDIT 2: Look at the point 4 at the beginning of the post - these are the events we're adding the handlers for.</code></li> </ol> <p>From the looks of it - it was done this way on purpose. Why? One can only guess...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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