如何将SelectedDateChanged事件从DatePicker绑定到VM中的Command

Mec*_*h0z 3 .net c# wpf

我有一个带有空代码隐藏文件的wpf文件(如果可能,我想保持代码隐藏为空)

http://pastebin.com/x1CTZDFK

然后我有了这个viewmodel文件

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,我希望SelectedDateChanged事件执行我的公共ICommand DateSelectionChanged_cmd,以便我可以在我的视图模型中验证它.我试着让System.Windows.Interactivity工作但是当我把它作为参考添加它不会编译并说它无法找到该文件,所以现在我想知道是否还有其他方法可以做到这一点,我写的方式是xml文件只是我猜它会看起来的一个例子(这是错误的方式)

Tho*_*que 7

您无法将事件直接绑定到命令.您可以使用此处所示的附加行为,但实际上您不需要:由于绑定SelectedDate是双向的,您只需要DateSelectionChangedExecuteSelectedDate属性的setter中执行:

    public DateTime SelectedDate
    {
        get
        {
            return selectedDate;
        }
        set
        {
            selectedDate = value;
            RaisePropertyChanged("SelectedDate");
            DateSelectionChangedExecute();
        }
    }
Run Code Online (Sandbox Code Playgroud)