2011-04-22 77 views
3

從一個datepicker一個SelectedDateChanged事件綁定到命令我有一個空的代碼隱藏文件,這WPF文件(我想保持代碼隱藏空的,如果可能的話)如何我在VM

http://pastebin.com/x1CTZDFK

我再有這樣的視圖模型文件

using System; 
using System.Collections.ObjectModel; 
using System.Windows.Input; 
using MVVM_Test.Model; 
using MvvmFoundation.Wpf; 

namespace MVVM_Test.ViewModel 
{ 
    public class ViewModel : ObservableObject 
    { 
     private DateTime selectedDate; 
     public DateTime SelectedDate 
     { 
      get 
      { 
       return selectedDate; 
      } 
      set 
      { 
       selectedDate = value; 
       RaisePropertyChanged("SelectedDate"); 
      } 
     } 

     private DateTime startDate; 
     public DateTime StartDate 
     { 
      get { return startDate; } 
      set 
      { 
       startDate = value; 
       RaisePropertyChanged("StartDate"); 
      } 
     } 

     private DateTime endDate; 
     public DateTime EndDate 
     { 
      get { return endDate; } 
      set 
      { 
       endDate = value; 
       RaisePropertyChanged("EndDate"); 
      } 
     } 

     public ObservableCollection<Brick> SavedBricks { get; set; } 

     public ViewModel() 
     { 
      SelectedDate = DateTime.Now; 
      StartDate = new DateTime(2011, 1, 1); 
      EndDate = new DateTime(2011, 7, 31); 
      SavedBricks = new ObservableCollection<Brick>(); 
      //Brick b1 = new Brick(DateTime.Now, 50,50,50,300); 
      //SavedBricks.Add(b1); 
     } 

     public ICommand PrevHistory_cmd 
     { 
      get { return new RelayCommand(PrevHistoryExecute, PrevHistoryCanExecute); } 
     } 

     private void PrevHistoryExecute() 
     { 
      SelectedDate = SelectedDate - new TimeSpan(1, 0, 0, 0); 
     } 

     private bool PrevHistoryCanExecute() 
     { 
      if (StartDate < SelectedDate) 
       return true; 
      return false; 
     } 

     public ICommand NextHistory_cmd 
     { 
      get { return new RelayCommand(NextHistoryExecute, NextHistoryCanExecute); } 
     } 

     private void NextHistoryExecute() 
     { 
      SelectedDate = SelectedDate + new TimeSpan(1, 0, 0, 0); 
     } 

     private bool NextHistoryCanExecute() 
     { 
      if(EndDate > SelectedDate) 
       return true; 
      return false; 
     } 

     public ICommand StartStopSort_cmd 
     { 
      get { return new RelayCommand(NextHistoryExecute, NextHistoryCanExecute); } 
     } 

     private void StartStopSortExecute() 
     { 

     } 

     private bool StartStopSortCanExecute() 
     { 
      return true; 
     } 

     public ICommand DateSelectionChanged_cmd 
     { 
      get { return new RelayCommand(NextHistoryExecute, NextHistoryCanExecute); } 
     } 

     private void DateSelectionChangedExecute() 
     { 

     } 

     private bool DateSelectionChangedCanExecute() 
     { 
      return true; 
     } 
    } 
} 

那麼我想SelectedDateChanged事件來執行我的公開的ICommand DateSelectionChanged_cmd,這樣我可以在我的視圖模型驗證。我試圖讓System.Windows.Interactivity工作,但當我將它作爲參考添加它不會編譯,並說它無法找到該文件,所以現在我想知道是否有任何其他方式來做到這一點,我寫的方式xml文件只是我如何猜測它會看起來的一個例子(哪種方法是錯誤的)

回答

7

您不能直接將事件綁定到命令。您可以使用附加的行爲如圖所示here,但實際上你並不需要:由於結合SelectedDate是雙向的,你只需要在SelectedDate setter方法執行DateSelectionChangedExecute

public DateTime SelectedDate 
    { 
     get 
     { 
      return selectedDate; 
     } 
     set 
     { 
      selectedDate = value; 
      RaisePropertyChanged("SelectedDate"); 
      DateSelectionChangedExecute(); 
     } 
    }