I want to do a two way bind from my ViewModel to selected date (single) in my XAML calendarview. But we have no dependency properties to do it.
What to do ?
EDIT: the code I tried but I got Error. Its the combination of these two topics: How to use CalenderView in UWP MVVM How to select rang of dates on finger slide on Calendar Control - UWP Win10 VS2015 XAML:
<CalendarView MinHeight="250" MaxHeight="500" MaxWidth="720" FontWeight="Normal"
DayItemFontWeight="Light" MinWidth="100"
SelectionMode="Single"
Style="{StaticResource Mahcalenderstyle}" Visibility="Visible"
HorizontalAlignment="Stretch" x:Name="MyCalendarView" DisplayMode="Month" Margin="9,25,9,0"
VerticalAlignment="Top" SelectedDatesChanged="CalendarView_SelectedDatesChanged"
CalendarViewDayItemStyle="{StaticResource CalendarViewDayItemStyle1}"
/>
And my VM:
private DateTimeOffset _selecteddate;
public DateTimeOffset SelectedDate
{
get
{
return _selecteddate;
}
set
{
if (_selecteddate != value)
{
_selecteddate = value;
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs("SelectedDate"));
}
}
}
}
public CalendarViewModel()
{
SelectedDate = DateTimeOffset.Now;}
The helper:
public static class CalendarViewHelper
{
public static IList<DateTimeOffset> GetSelectedDates(DependencyObject obj)
{
return (IList<DateTimeOffset>)obj.GetValue(SelectedDatesProperty);
}
public static void SetSelectedDates(DependencyObject obj, IList<DateTimeOffset> value)
{
obj.SetValue(SelectedDatesProperty, value);
}
public static readonly DependencyProperty SelectedDatesProperty =
DependencyProperty.RegisterAttached("SelectedDates", typeof(IList<DateTimeOffset>), typeof(CalendarView),
new PropertyMetadata(null, (d, e) =>
{
var cv = d as CalendarView;
var dates = e.NewValue as IList<DateTimeOffset>;
if (cv != null && dates != null)
{
foreach (var date in dates)
{
cv.SelectedDates.Add(date);
}
}
}));
}