1

The code below executes fine when MyActionFunc is called but not when the function is in another class. MessageBox displays the correct string but it is not shown on view. What I am missing?

class ViewModel : INotifyPropertyChanged
{
    public MyCommand ActionCommand
    {
        get;
        set;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    
    protected void OnPropertyChanged([CallerMemberName] string propertyname = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
    }

    public ViewModel()
    {
        ActionCommand = new MyCommand();
        ActionCommand.CanExecuteFunc = obj => true;
       // ActionCommand.ExecuteFunc = MyActionFunc;
        ActionCommand.ExecuteFunc = MyClass.MyActionFunc;
    }
   
    private string myname;
    public string myName
    {
        get => myname;
        set { myname = value;; OnPropertyChanged(); }
    }

    public void MyActionFunc(object parameter)
    {
        myName = "Fred";
    }
}
class MyClass
{
    public static void MyActionFunc(object parameter)
    {
        ViewModel name = new ViewModel();
        name.myName = "Fred";
        MessageBox.Show(name.myName);
    }
}

... and the binding to the Textbox

        <TextBox Name="textBox" Grid.Column="1"  Grid.Row="1" Text="{Binding Path=myName,  Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>
Mat52
  • 21
  • 2
  • You would need to pass the actual ViewModel as `parameter` for that to work. Otherwise you are setting the `myName` property of a new viewModel instance, which isn't bound. – Fildor Feb 14 '22 at 12:15
  • how could this be achieved? I realize that by calling **new** another memory cell on the stack will be allocated. But how can I pass the reference of the original instance to other method? – Mat52 Feb 14 '22 at 16:25
  • On the heap, not stack. On _how to pass_ it ... errr my WPF skills are tooo rusty, I am afraid. I would _guess_ there should be a way to bind it to the parameter via XAML ... but really, it's been ages since I've dealt with that. – Fildor Feb 14 '22 at 16:26

0 Answers0