Mech0z
Mech0z

Reputation: 3647

How do I bind a SelectedDateChanged event from a DatePicker to a Command in my VM

I have this wpf file with an empty codebehind file (And I would like to keep the codebehind empty if possible)

http://pastebin.com/x1CTZDFK

And I then have this viewmodel file

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

I then want the SelectedDateChanged event to execute my public ICommand DateSelectionChanged_cmd so that I can validate it in my view model. I tried getting the System.Windows.Interactivity to work but when I add it as a reference it wont compile and says it cant find the file, so now I wonder if there is any other way to do this, the way I wrote it in the xml file is just an example of how I would guess it would look (Which is the wrong way)

Upvotes: 3

Views: 6085

Answers (1)

Thomas Levesque
Thomas Levesque

Reputation: 292425

You can't bind an event directly to command. You could use an attached behavior as shown here, but actually you don't need to: since the binding SelectedDate is two-way, you just need to execute DateSelectionChangedExecute in the setter of the SelectedDate property:

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

Upvotes: 8

Related Questions