1

I have created the InvokeCommandAction event programmatically like this link. But I don't know how to create the below event programmatically,

 <i:Interaction.Triggers>
        <i:EventTrigger EventName="Drop">                                    
               <cmd:EventCommandExecuter Command="{Binding AddGridCommand}" />
        </i:EventTrigger>
 </i:Interaction.Triggers>

I have tried like below, but null is returned in the EventCommandExecuter Command. Please anyone suggest me to achieve this.

void SetGridTrigger(Grid itemsControl, string bindingPath)
{                     
    var binding = new Binding { Path = new PropertyPath(bindingPath) };
    ICommand DropCommand = (binding as ICommand);
    var eventCommandExecuter = new EventCommandExecuter { Command = DropCommand };

    BindingOperations.SetBinding(eventCommandExecuter, InvokeCommandAction.CommandProperty, binding);

    var eventTrigger = new System.Windows.Interactivity.EventTrigger { EventName = "Drop" };
    eventTrigger.Actions.Add(eventCommandExecuter);

    var triggers = Interaction.GetTriggers(itemsControl);
    triggers.Add(eventTrigger);
}

EventCommandExecuter

 public class EventCommandExecuter : TriggerAction<DependencyObject>
    {
        #region Constructors

        public EventCommandExecuter()
            : this(CultureInfo.CurrentCulture)
        {
        }

        public EventCommandExecuter(CultureInfo culture)
        {
            Culture = culture;
        }

        #endregion

        #region Properties

        #region Command

        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }

        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.Register("Command", typeof(ICommand), typeof(EventCommandExecuter), new PropertyMetadata(null));

        #endregion

        #region EventArgsConverterParameter

        public object EventArgsConverterParameter
        {
            get { return (object)GetValue(EventArgsConverterParameterProperty); }
            set { SetValue(EventArgsConverterParameterProperty, value); }
        }

        public static readonly DependencyProperty EventArgsConverterParameterProperty =
            DependencyProperty.Register("EventArgsConverterParameter", typeof(object), typeof(EventCommandExecuter), new PropertyMetadata(null));

        #endregion

        public IValueConverter EventArgsConverter { get; set; }

        public CultureInfo Culture { get; set; }

        #endregion

        protected override void Invoke(object parameter)
        {
            var cmd = Command;

            if (cmd != null)
            {
                var param = parameter;

                if (EventArgsConverter != null)
                {
                    param = EventArgsConverter.Convert(parameter, typeof(object), EventArgsConverterParameter, CultureInfo.InvariantCulture);
                }

                if (cmd.CanExecute(param))
                {
                    cmd.Execute(param);
                }
            }
        }
    }
Yogeshwaran
  • 170
  • 12

1 Answers1

2

You should bind the EventCommandExecuter.CommandProperty to your binding path:

EventCommandExecuter eventCommandExecuter = new EventCommandExecuter();
BindingOperations.SetBinding(eventCommandExecuter, EventCommandExecuter.CommandProperty,
    new Binding { Path = new PropertyPath(bindingPath) });

EventTrigger eventTrigger = new EventTrigger { EventName = "Drop" };
eventTrigger.Actions.Add(eventCommandExecuter);
Interaction.GetTriggers(itemsControl).Add(eventTrigger);
mm8
  • 163,881
  • 10
  • 57
  • 88