Search code examples
c#uwptextblockcalendarview

Obtain currently selected datein CalendarView [UWP] [C#]


In my UWP C# app I want to display the currently selected date of a CalendarView in a TextBlock, I know I should use the SelectedDatesChanged event to update the TextBlock but I cannot find any code to obtain and parse the date.


Solution

  • You can use args.AddedDates to get Date from SelectedDatesChanged event

    private void CalendarView_SelectedDatesChanged(CalendarView sender, CalendarViewSelectedDatesChangedEventArgs args)
    {
        var myDate = args.AddedDates.First();  //Since args.AddedDates returns collection we should use First to get the first item
        MyTextBlock.Text = myDate.ToString();  //You can convert DateTime into different format using myDate.ToString(format);
    }
    

    To learn more about Use patterns to format dates and times, DateTimeOffset.ToString Method