How can I pass a class instance to a method outside this class (MVVM)

Mat11

New member
Joined
Feb 16, 2022
Messages
2
Location
Germany
Programming Experience
10+
Hello all,
not sure if this is the right place to put this thread on. I hope to get an idea of the following issue:
From a ViewModel I want to call a method in a class different to the calling class but within the same namespace, as below:

C#:
namespace WpfApp8
{
    class ViewModel : INotifyPropertyChanged
    {
    ...
        public ViewModel()
        {
          
            ActionCommand = new MyCommand(this);
            ActionCommand.CanExecuteFunc = obj => true;
            //ActionCommand.ExecuteFunc = MyActionFunc;
            ActionCommand.ExecuteFunc = MyClass.MyActionFunc ;
        }    
      
        private string myname;
        public string myName
        {
            get => myname;
            set { myname = value; OnPropertyChanged(); }
        }
   ...
   }

   public class MyClass
   {
            
        public static void MyActionFunc(object param)
        {
             _ = MessageBox.Show("Parameter: " + (string)param);
         
             param.myName = "Fred";
        }
   }

}

The method is invoked by a button click action in the View

View:
<Button
            Command="{Binding Path=ActionCommand }"
            CommandParameter="{ ?? }"
            Grid.Column="1" Grid.Row="2">
            Click
</Button>

I have no idea how to set the CommandParameter in oder to get the instance of the ModelView transferred to the method.
Any hint is much appreciated.
Matthias
 
Unless you are specifically trying to use the MVVM pattern in WinForms, I'm going to move this over into the WPF subforum.
 
Your MyClass.MyFunction() take an object parameter that you seem to be just casting as a string. What is that string that you are trying to pass in? Does it come from the view model, or some place else?
 
Unless you are specifically trying to use the MVVM pattern in WinForms, I'm going to move this over into the WPF subforum.
This is the command class from where the execute function is implemented. The code works fine when I place public void MyActionFunc(object param) into the ViewModel class.
C#:
    class MyCommand : ICommand
    {
        public Predicate<object> CanExecuteFunc
        {
            get;
            set;
        }

        public Action<object> ExecuteFunc
        {
            get;
            set;
        }     

        public bool CanExecute(object parameter)
        {
            return CanExecuteFunc(parameter);          
        }

        public void Execute(object parameter)
        {
           
            ExecuteFunc(parameter);
        }

        public event EventHandler CanExecuteChanged;
    }

Also when passing a simple string in the MainWindow.xaml button tag let say as CommandParameter ="Hello World" it works. I have no clue how to get the instance of the viewmodel into that parameter tag.
 
In other words, you are looking for the binding syntax that references the entire view model instead of just a property of the view model. I'm blanking on that right now. Somewhere in the back of my memory is something that makes me think that I had read how to do that once upon a time. Alas old age plus anesthesia amnesia is not helping with the recall. I suggest reading through MS's data binding docs. When I have more time, I probably will do the same. Alternatively, more targeted web searches may give a direct answer. I'm quite sure you aren't the first to ask something similar.
 

Latest posts

Back
Top Bottom