Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can create abstract CallbackableCommand that would raise callback method.</p> <pre><code>abstract class CallbackableCommand : ICommand { private IInputElement getRaisedElement() { return Keyboard.FocusedElement; } public void Execute(object parameter) { ExecuteImpl(parameter); var element = getRaisedElement(); if(element == null) return; //var ci = typeof(ExecutedRoutedEventArgs).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)[0]; //var routedEventArgs = (RoutedEventArgs)ci.Invoke(new object[] { this, parameter }); var routedEventArgs = new RoutedEventArgs(); //routedEventArgs.RoutedEvent = CommandManager.ExecutedEvent; routedEventArgs.RoutedEvent = Callbackable.CommandExecutedEvent; routedEventArgs.Source = element; routedEventArgs.Handled = false; element.RaiseEvent(routedEventArgs); } public abstract void ExecuteImpl(object parameter); abstract public bool CanExecute(object parameter); abstract public event EventHandler CanExecuteChanged; } </code></pre> <p>Inherit your command from CallbackableCommand and override CanExecute, CanExecuteChanged and ExecuteImpl(instead of Execute)</p> <pre><code> class SimpleCommand : CallbackableCommand { public override void ExecuteImpl(object parameter) { MessageBox.Show("Simple command execute with parameter: " + parameter ?? "null"); } public override bool CanExecute(object parameter) { return true; } public override event EventHandler CanExecuteChanged; } </code></pre> <p>Own element to specify <code>CommandExecuted</code> event:</p> <pre><code> public class Callbackable : ContentControl { public static readonly RoutedEvent CommandExecutedEvent = EventManager.RegisterRoutedEvent( "CommandExecuted", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Callbackable)); // Provide CLR accessors for the event public event RoutedEventHandler CommandExecuted { add { AddHandler(CommandExecutedEvent, value); } remove { RemoveHandler(CommandExecutedEvent, value); } } } </code></pre> <p><strong>Edit:</strong> In your control specify <code>Callbackable.CommandExecuted</code> event</p> <pre><code>&lt;Grid&gt; &lt;Grid.Resources&gt; &lt;local:SimpleCommand x:Key="btnCommand" /&gt; &lt;/Grid.Resources&gt; &lt;local:Callbackable&gt; &lt;Button Command="{StaticResource btnCommand}" CommandParameter="param" local:Callbackable.CommandExecuted="Button_Executed" &gt; Click me &lt;/Button&gt; &lt;/local:Callbackable&gt; &lt;/Grid&gt; </code></pre> <p>Executed event handler:</p> <pre><code>private void Button_Executed(object sender, ExecutedRoutedEventArgs e) { MessageBox.Show("Executed"); } </code></pre>
    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.
 

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