Search code examples
silverlightmvvmtoolkit

MVVM-light and Windows Phone Toolkit - RelayCommand<DateTimeValueChangedEventArgs> failing


I'm trying to folow the mvvm pattern. When using galasoft EventToCommand i'm getting then following error: The best overloaded method match for 'GalaSoft.MvvmLight.Command.RelayCommand.RelayCommand(System.Action)' has some invalid arguments...

Code from my XAML:

<toolkit:DatePicker Header="Select Date" 
     ValueStringFormat="{}{0:D}"                                    
     HorizontalAlignment="Left" Margin="0,126,0,0" 
     Name="datePicker1" 
     VerticalAlignment="Top" FontFamily="Verdana"  
     FontSize="22" Width="450">
     <i:Interaction.Triggers>
          <i:EventTrigger EventName="ValueChanged">
               <cmd:EventToCommand PassEventArgsToCommand="True"
                     Command="{Binding DateSelection}"/>
          </i:EventTrigger>
     </i:Interaction.Triggers>
</toolkit:DatePicker>

In the modelview:

  public MainViewModel()
    {
        DateSelection = new RelayCommand<DateTimeValueChangedEventArgs>(time_Call);
    }

    public RelayCommand<DateTimeValueChangedEventArgs> DateSelection
    {
        get;
        set;
    }
    void time_Call(object sender, DateTimeValueChangedEventArgs e)
    {

    }

I'm blank!


Solution

  • Can you Two-Way bind to the Value property instead? This would simplify things and let you use the true power of XAML and MVVM... binding.

    <toolkit:DatePicker Header="Select Date" 
         ValueStringFormat="{}{0:D}"                                    
         HorizontalAlignment="Left" Margin="0,126,0,0" 
         Name="datePicker1" 
         VerticalAlignment="Top" FontFamily="Verdana"  
         FontSize="22" Width="450"
    Value={Binding SelectedDate, Mode=TwoWay}" />
    

    The view model

      private DateTime selectedDate;
        public DateTime SelectedDate
        {
            get
            {
              return this.selectedDate;
            }
    
            set
            {
              if (this.selectedDate != value)
              {
                this.selectedDate = value;
                this.RaisePropertyChanged("SelectedDate");
              }
            }
        }
    
        public MainViewModel()
        {
    // initialize to today being selected
    this.SelectedDate = DateTime.Now;
    // the property changed might not be necessary if you are just trying to get the new value
        this.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(MainViewModel_PropertyChanged);
        }
    
        void MainViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
          if(e.PropertyName="SelectedDate")
        {
        // do something if needed
        }
        }