C# ICommand Interface Help

Hi,

I have a WPF application and I have been trying to implement a MVVM design. I have used the ICommand interface, which works fine however I do not really understand how it works.

Here is my code, the question is beneath the code blocks.

MyCommands Classs
public class MyCommands : ICommand
    {
        public delegate void ICommandOnExecute(object parameter);
        public delegate bool ICommandOnCanExecute(object parameter);

        private ICommandOnExecute _execute;
        private ICommandOnCanExecute _canExecute;

        public MyCommands(ICommandOnExecute onExecuteMethod, ICommandOnCanExecute onCanExecuteMethod)
        {
            _execute = onExecuteMethod;
            _canExecute = onCanExecuteMethod;
        }

        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public bool CanExecute(object parameter)
        {
            return _canExecute.Invoke(parameter);
        }

        public void Execute(object parameter)
        {
            _execute.Invoke(parameter);
        }
    }

Open in new window


Part of my ViewModel code
public ICommand CommandButtImport { get; set; }                     // for the button that imports the orders file

// constructor
        public ViewModel() 
        {
            CommandButtImport = new MyCommands(ExecuteCommandButtImport, CanExecuteCommandButtImport);
        }

        // import button
        private bool CanExecuteCommandButtImport(object parameter)
        {
            return true;
        }

        // import button
        private void ExecuteCommandButtImport(object parameter)
        {
          // some logic                       
         }

Open in new window


So the "Import" button works fine. I have another two events that I need to implement the MVVM design, both below in the code block. The first is a button however I need to make use of the RoutedEventArgs variable, which is where I start to get confussed as for my implmentation of the "Import" button above it recieves a paramter of type object and when I debug its always null? How do I do this?

private void RowHeaderToggleButton_Click(object sender, RoutedEventArgs e)
{
  // some logic
}

private void dataGridOrders_RowDetailsVisibilityChanged(object sender, DataGridRowDetailsEventArgs e)
{
  // some logic
}

Open in new window


Any help would be great!
Thanks,
M
mcs26Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ExpertHearCommented:
If you need RoutedEventArgs, either you can forget about the command binding on the button and just use the Click event handler in code behind. Because there is no straight forward way to pass RoutedEventArgs as CommandParameter.

Or apparently you can use attached behavior to achieve your goals, please refer to this post:
http://stackoverflow.com/questions/18709883/how-to-add-the-routedeventargs-as-the-command-parameter
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.