2014-03-27 55 views
0

我正在做.net中的第一個項目,並且我嘗試使用MVVM模式。我如何在我的ViewModel和我的ICommand類之間共享數據?我的視圖模型是這樣的:在ICommand實現,ViewModel和View之間共享數據

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作爲內部類,但我也嘗試過的「獨立」級,但不能讓它工作之一。我想讓我的命令「測試」與WorkClass屬性BookList和Mybook一起使用和共享數據。 (書是我只包含一些字符串屬性定義的類)

我的應用程序現在有兩個窗口,在主窗口中創建WorkClass的實例並將其插入到它的datacontext。然後,我將其穿過的DataContext像這樣其他的窗口(這是正確的?):

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

我想從這些窗口我的命令,從該WorkClass實例執行上的數據變化。我希望我清楚自己在問什麼。我知道最好使用可通過互聯網獲得的RelayCommand或DelegateCommand類,但由於這是項目到學校,所以恐怕會被視爲抄襲其他人的工作。

如果您需要我從代碼中發佈更多內容或解釋我將編輯的任何內容。

謝謝你的幫助。

+0

你有一個參數對你的命令的方法 - 這是否是不夠的? –

+0

什麼不行? –

+0

好吧,它的工作方式,但這種方式我不知道如何使用Execute方法中WorkClass的數據。當我有時間時,我會嘗試Viacheslav的答案:) – xyann00

回答

0

您需要通過一個視圖模型實例初始化命令,並委託命令執行到視圖模型類

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

}