Lasse Edsvik
Lasse Edsvik

Reputation: 9298

Refresh Form on DatePicker change

I'm trying to refresh the data in a Form when the user changes the Date using the MVVM pattern.

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">
....

ViewModel:

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

How shall I connect the EntryDate_Selected with the "refresh" of the object in the viewmodel?

Upvotes: 1

Views: 156

Answers (1)

crunchytortoise
crunchytortoise

Reputation: 91

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();
}

I looked at the docs here: https://msdn.microsoft.com/en-us/library/system.windows.forms.monthcalendar.dateselected(v=vs.110).aspx

The string can be formatted however you'd like.

Upvotes: 1

Related Questions