【问题标题】:Share data between ICommand implementation, ViewModel and View在 ICommand 实现、ViewModel 和 View 之间共享数据
【发布时间】:2014-05-06 06:48:33
【问题描述】:

我正在.net 中做我的第一个项目,我正在尝试使用 MVVM 模式。如何在 ViewModel 和 ICommand 类之间共享数据?我的 ViewModel 如下所示:

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));
        }
    }
}

我将 ICommand 实现为内部类,但我也尝试了一个“独立”类,但也无法让它工作。我希望我的命令“test”能够与 WorkClass 属性 BookList 和 Mybook 一起使用并共享数据。 (书是我定义的只包含一些字符串属性的类)

我的应用程序现在有两个窗口,在主窗口中我创建了 WorkClass 的实例并将其插入到它的数据上下文中。然后我通过 datacontext 将它传递给像这样的其他窗口(这是正确的吗?):

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

我希望这些窗口中的命令对来自此 WorkClass 实例的数据执行更改。我希望很清楚我在问什么。我知道最好使用 Internet 上提供的 RelayCommand 或 DelegateCommand 类,但由于这是学校的项目,我担心它会被视为抄袭别人的工作。

如果您需要我从代码中发布更多内容或解释我将编辑的任何内容。

感谢您的帮助。

【问题讨论】:

  • 您的命令中有一个参数 - 这还不够吗?
  • 它可以工作,但是这样我不知道如何在 Execute 方法中处理来自 WorkClass 的数据。有时间我会试试维亚切斯拉夫的答案:)

标签: c# wpf mvvm icommand


【解决方案1】:

您需要通过视图模型实例初始化命令并将命令执行委托给视图模型类的方法

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));
    }
}

}

【讨论】:

    猜你喜欢
    • 2019-07-02
    • 2011-05-24
    • 1970-01-01
    • 2015-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2010-11-12
    相关资源
    最近更新 更多