2017-12-14 5 views
0

を使用してxamarinフォームでViewModelにするために選択した日付をバインドするには、以下のように私は、日付ピッカーを持っている:私はDateSelectedバインディングを追加するとき、私は私のViewModelに新たに選択した日付を取得したいが、どのように日付ピッカー

<DatePicker x:Name="MyDatePicker" Grid.Row="0" Grid.Column="1" HorizontalOptions="Center" VerticalOptions="Center" Date="{Binding BookingDate}" Format="dd/MM/yyyy" /> 

DateSelected="{Binding MyViewModelJob}" 

これは動作しません(DatePickerが開いているページでさえ開かれません)。

XAMLとViewModelで日付を取得するにはどうすればよいですか?

+0

'DateSelected =" new_dateSelected "と' private void new_dateSelected(オブジェクト送信者、DateChangedEventArgs e){//あなたのことを実行} 'を試してください。 [DatePicker DateSelectedイベントに基づいて、日付が変更されない場合、発生しない?](https://forums.xamarin.com/discussion/47570/datepicker-dateselected-event-not-fired-if-the-date-doesnt-変化する) –

答えて

1

DateSelectedはバインド可能なプロパティではなく、DatePickerタイプのイベントです。 viewmodelのプロパティを使用して日付を変更する場合は、DatePickerのDateプロパティに既に関連付けられている「BookingDate」プロパティを変更するだけで済みます。

HERESにいくつかの擬似コード:

public class YourViewModel : INotifyPropertyChanged //Implement INPC to update the view when a property changes 
{ 

    private DateTime bookingDate; 
    public DateTime BookingDate 
    { 
     get { return bookingDate; } 
     set 
     { 
      bookingDate = value; 
      OnPropertyChanged("BookingDate"); //Call INPC Interface when property changes, so the view will know it has to update 
     } 
    } 

    private void ChangeDate(DateTime newDate) 
    { 
     BookingDate = newDate; //Assing your new date to your property 
    } 
} 
1

日付ピッカーでは、日付です。これはバインド可能なプロパティです。 XAMLで

<DatePicker 
     HeightRequest="40" 
     Date ="{Binding StartDate}" 
/> 

とのViewModelにあなたが書くことができます

public class YourViewModel : INotifyPropertyChanged 
{ 
    DateTime _startdate; 
    public DateTime StartDate 
    { 
     get 
     { 
       return _startdate; 
     } 
     set 
     { 
      _startdate = value; 
      RaisePropertyChanged("StartDate"); 

     } 
    } 
    public event PropertyChangedEventHandler PropertyChanged; 

    protected void RaisePropertyChanged(string propertyName) 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 
をそして、あなたはあなたの日付を取得または設定することができますので、この後、あなたのViewModelの全てで、開始日を使用することができます。

関連する問題