How can I use InvokeCommandAction to call my method and pass parameters?

I was trying to figure out how to pass parameters from an event Loaded=""

. I asked the question here: How do I go about passing the parameter to Loaded = ""? and was heading towards InvokeCommandAction.

The problem is I can't 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 code that looks like this:

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

      

And Enum in 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 wrote above? I'm very new to WPF, so please try and bear with me if I end up asking what seem to be stupid questions. I've been browsing stackoverflow looking at other similar questions and haven't been able to enlighten enough information to proceed on my own.

+3


source to share


1 answer


Command binding requires a specific instance that implements the ICommand interface. You are binding to a method name that won't bind at all. Command binding is intended to be used in the ViewModel class in MVVM design, but from your example code, it appears that you are using it in code to represent the Xaml. If you want to stick with the code, just use an event handler.

There are many examples of ICommand implementation, you can also use from DelegateCommand in Prism . I show a simple example below that implements a very basic ICommand that will work for what you are trying to do as long as your View and ViewModel are connected.

    //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>

      

+4


source







All Articles