简体   繁体   中英

How do I go about using InvokeCommandAction to call a method of mine and pass in parameters?

I've been trying to figure out how to pass parameters from the Loaded="" event. I asked a question here: How would I go about passing a parameter on Loaded=" "? and was guided in the direction of InvokeCommandAction.

Issue is I am unable to figure out how to actually use the InvokeCommandAction to call my method. My XAML:

        <Expander x:Name="Greeting_And_Opening_Expander" ExpandDirection="Down" IsExpanded="True" FontSize="14" FontWeight="Bold" Margin="5" BorderThickness="1" BorderBrush="#FF3E3D3D">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Loaded">
                    <i:InvokeCommandAction Command="{Binding ExpanderLoaded}"
                                           CommandParameter="x:Static local:Sections.Opening"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>

I have a method named ExpanderLoaded in the code behind that goes as follows:

    private void ExpanderLoaded(object sender, RoutedEventArgs e, Sections section)
    {
        //Do Stuff
    }

And an Enum under the same namespace:

public enum Sections
{
    Default = 0,
    Opening = 1,
    Verify = 2
}

What do I need to do to call my method using the XAML I posted above? I am very new to WPF, so please try and bear with me if I end up asking what seem to be stupid questions. I've browsed around stackoverflow reading other similar questions and have been unable to gleam enough information to continue on my own.

Command Binding requires a concrete instance that implements ICommand interface. You are binding to a method name, which will not really bind at all. Command binding is meant to be used in ViewModel class within MVVM design, but from your sample code it seems you are using it within code-behind of the Xaml view. If you want to stick to code-behind, just use event handler.

There are lot of examples on ICommand implementation, you can also use out of the box DelegateCommand available in Prism . I am showing a simple example below which implements a very basic ICommand that will work for what you are trying to do as long as your View and ViewModel is wired up.

    //Very basic ICommand implementation    
    public class RelayCommand : ICommand
    {
        private Action<object> command;
        private Func<bool> canExecute;

        public RelayCommand(Action<object> commandAction, Func<bool> canExecute = null)
        {
            this.command = commandAction;
            this.canExecute = canExecute;
        }

        /// <summary>
        /// Returns default true. 
        /// Customize to implement can execute logic.
        /// </summary>
        public bool CanExecute(object parameter)
        {
            return this.canExecute == null ? true : this.canExecute();
        }

        /// <summary>
        /// Implement changed logic if needed
        /// </summary>
        public event EventHandler CanExecuteChanged;


        public void Execute(object parameter)
        {            
            if (this.command != null)
            {
                this.command(parameter);
            }
        }
    }

    //Example of a view model
    public class MyViewModel
    {
        public MyViewModel()
        {
            this.ExpanderCommand = new RelayCommand(this.ExecuteExpanderCommand);
        }

        // This property will be the command binding target
        public RelayCommand ExpanderCommand { get; set; }

        // this is the handler method
        public void ExecuteExpanderCommand(object parameter)
        {
            var section = (Sections)parameter;
            //do your stuff here
        }
    }

Xaml binding:

<i:EventTrigger EventName="Loaded">
 <i:InvokeCommandAction Command="{Binding ExpanderCommand}"
                                               CommandParameter="x:Static local:Sections.Opening"/>
</i:EventTrigger>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM