/// Container class for attached properties. Must inherit from <see cref="AvaloniaObject"/>.
public class DoubleTappedBehav : AvaloniaObject
static DoubleTappedBehav()
CommandProperty.Changed.Subscribe(x => HandleCommandChanged(x.Sender, x.NewValue.GetValueOrDefault<ICommand>()));
/// Identifies the <seealso cref="CommandProperty"/> avalonia attached property.
/// <value>Provide an <see cref="ICommand"/> derived object or binding.</value>
public static readonly AttachedProperty<ICommand> CommandProperty = AvaloniaProperty.RegisterAttached<DoubleTappedBehav, Interactive, ICommand>(
"Command", default(ICommand), false, BindingMode.OneTime);
/// Identifies the <seealso cref="CommandParameterProperty"/> avalonia attached property.
/// Use this as the parameter for the <see cref="CommandProperty"/>.
/// <value>Any value of type <see cref="object"/>.</value>
public static readonly AttachedProperty<object> CommandParameterProperty = AvaloniaProperty.RegisterAttached<DoubleTappedBehav, Interactive, object>(
"CommandParameter", default(object), false, BindingMode.OneWay, null);
/// <see cref="CommandProperty"/> changed event handler.
private static void HandleCommandChanged(IAvaloniaObject element, ICommand commandValue)
if (element is Interactive interactElem)
if (commandValue != null)
interactElem.AddHandler(InputElement.DoubleTappedEvent, Handler);
interactElem.RemoveHandler(InputElement.DoubleTappedEvent, Handler);
void Handler(object s, RoutedEventArgs e)
// This is how we get the parameter off of the gui element.
object commandParameter = interactElem.GetValue(CommandParameterProperty);
if (commandValue?.CanExecute(commandParameter) == true)
commandValue.Execute(commandParameter);
/// Accessor for Attached property <see cref="CommandProperty"/>.
public static void SetCommand(AvaloniaObject element, ICommand commandValue)
element.SetValue(CommandProperty, commandValue);
/// Accessor for Attached property <see cref="CommandProperty"/>.
public static ICommand GetCommand(AvaloniaObject element)
return element.GetValue(CommandProperty);
/// Accessor for Attached property <see cref="CommandParameterProperty"/>.
public static void SetCommandParameter(AvaloniaObject element, object parameter)
element.SetValue(CommandParameterProperty, parameter);
/// Accessor for Attached property <see cref="CommandParameterProperty"/>.
public static object GetCommandParameter(AvaloniaObject element)
return element.GetValue(CommandParameterProperty);