0

我正在尝试遵循 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)
    {

    }

我是空白!

4

1 回答 1

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
    }
    }
于 2012-05-30T15:56:31.477 回答