DatePicker上的刷新表单更改

时间:2018-02-02 15:33:04

标签: xaml xamarin mvvm

当用户使用MVVM模式更改Form时,我尝试刷新Date中的数据。

的Xaml:

<DatePicker DateSelected="EntryDate_Selected" 
    x:Name="EntryDate" Date="{Binding MoodEntry.EntryDate, Mode=TwoWay}">
<Slider x:Name="SliderSleep" Value="{Binding MoodEntry.Sleep, Mode=TwoWay}"
    Minimum="0" Maximum="5">
....

视图模型:

public class RegisterViewModel : INotifyPropertyChanged
{
    public MoodEntry MoodEntry { get; set; }
}

public RegisterViewModel()
{
    MoodEntry = App.MoodDatabase.GetMoodEntry(DateTime.Today);
    if (MoodEntry == null)
       MoodEntry = new MoodEntry();

    LowerLimitDate = new DateTime(2018, 1, 1);
    HighLimitDate = DateTime.Today;
}

MoodEntry.cs

namespace myMood.Models
{
    [Table("MoodEntries")]
    public class MoodEntry
    {
        [PrimaryKey, AutoIncrement, Column("MoodEntryID")]
        public int MoodEntryID { get; set; }

        [Indexed]
        public DateTime EntryDate { get; set; }
...

Register.xaml.cs:

private void EntryDate_Selected(object sender, ValueChangedEventArgs e) 
{
      // What goes here?
}

如何将EntryDate_Selected与&#34; refresh&#34;连接起来? viewmodel中的对象?

1 个答案:

答案 0 :(得分:1)

private void EntryDate_Selected(object sender, DateRangeEventArgs e)
{
    // Show the start and end dates in the text box.
    this.EntryDate.Text = "Date Selected: Start = " +
    e.Start.ToShortDateString() + " : End = " + e.End.ToShortDateString();
}

我在这里查看了文档:https://msdn.microsoft.com/en-us/library/system.windows.forms.monthcalendar.dateselected(v=vs.110).aspx

可以根据需要格式化字符串。