简体   繁体   中英

Share data between ICommand implementation, ViewModel and View

I am doing my first project in .net and I am trying to use MVVM pattern. How can I share data between my ViewModel and my ICommand class? My ViewModel look like this:

public class WorkClass : INotifyPropertyChanged
{

    public event PropertyChangedEventHandler PropertyChanged;
    private Book mybook;
    private ObservableCollection<Book> bookList;

    public Book Mybook
    {
        get { return mybook; }
        set
        {
            book = value;
            OnPropertyChanged("Mybook");
        }
    }

    public ObservableCollection<Book> BookList
    {
        get { return bookList; }
        private set
        {
            bookList = value;
            OnPropertyChanged("BookList");
        }
    }

    testCommand2 test = new testCommand2();

    public ICommand Test { get { return test; } }

    private class testCommand2 : ICommand
    {

        public void Execute(object parameter)
        {
            // Do stuff here
        }

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

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

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}

I implemented ICommand as inner class, but I tried also an "stand-alone" class but couldn't get it work either. I want my command "test" to work with and share data with WorkClass properties BookList and Mybook. (Book is class I defined containing only some string properties)

My app has two windows now, in the mainwindow I create instance of WorkClass and insert it to its datacontext. Then I pass it through datacontext to other window like this (is this correct?):

Window1 w = new Window1();
w.DataContext = this.DataContext;
w.ShowDialog();

I want my commands from those windows to perform changes on the data from this WorkClass instance. I hope it is clear what I am asking. I know it would be best to use RelayCommand or DelegateCommand classes which are available through the internet, but since this is project to the school I am afraid it would be taken as copying someone else's work.

If you need me to post some more from the code or explain anything I will edit.

Thank you for help.

You need to initialize a command by a view model instance and delegate command execution to the method of the View Model class

public class WorkClass : INotifyPropertyChanged
{

    public WorkClass()
    {
        test = new testCommand2(this);
    }

    // Will be called on command execturion
    public Test2Action()
    {
    }

public event PropertyChangedEventHandler PropertyChanged;
private Book mybook;
private ObservableCollection<Book> bookList;

public Book Mybook
{
    get { return mybook; }
    set
    {
        book = value;
        OnPropertyChanged("Mybook");
    }
}

public ObservableCollection<Book> BookList
{
    get { return bookList; }
    private set
    {
        bookList = value;
        OnPropertyChanged("BookList");
    }
}

testCommand2 test;

public ICommand Test { get { return test; } }

private class testCommand2 : ICommand
{
    private readonly WorkClass _wc;

    public testCommand2(WorkClass wc)
    {
       _wc = wc;
    }

    public void Execute(object parameter)
    {
        _wc.Test2Action();
    }

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

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

protected void OnPropertyChanged(string name)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(name));
    }
}

}

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