MVVM-light和Windows Phone Toolkit - RelayCommand <datetimevaluechangedeventargs>失败</datetimevaluechangedeventargs>

时间:2012-05-30 15:03:52

标签: silverlight mvvm toolkit

我正试图遵循mvvm模式。当使用galasoft EventToCommand时,我得到以下错误: 'GalaSoft.MvvmLight.Command.RelayCommand.RelayCommand(System.Action)'的最佳重载方法匹配有一些无效的参数......

我的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>

在模型视图中:

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

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

    }

我是空白的!

1 个答案:

答案 0 :(得分:0)

您可以双向绑定到Value属性吗?这将简化事情并让您使用XAML和MVVM ...绑定的真正力量。

<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}" />

视图模型

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