C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
And: We use the HasValue property on the nullable type to determine whether the inner DateTime exists or not.
Then: We get a reference to the DateTime indicated by the Calendar at this point. We call ToShortDateString on it.
Example markup: XAML
<Window x:Class="WpfApplication21.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Calendar
HorizontalAlignment="Left"
Margin="10,10,0,0"
VerticalAlignment="Top"
SelectedDatesChanged="Calendar_SelectedDatesChanged"/>
</Grid>
</Window>
Example code: C#
using System;
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication21
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Calendar_SelectedDatesChanged(object sender,
SelectionChangedEventArgs e)
{
// ... Get reference.
var calendar = sender as Calendar;
// ... See if a date is selected.
if (calendar.SelectedDate.HasValue)
{
// ... Display SelectedDate in Title.
DateTime date = calendar.SelectedDate.Value;
this.Title = date.ToShortDateString();
}
}
}
}