如何在 UWP MVVM 中使用 CalenderView [英] How to use CalenderView in UWP MVVM
问题描述
我想绑定选定的日历视图项并将其设置为一个日期时间变量.
I want to bind the selected Calender View Item and set it to a DateTime Variable.
我的 CalenderView Xaml 看起来像:
My CalenderView Xaml looks like:
<CalendarView Grid.Row="6" Grid.ColumnSpan="2" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="20"/>
我在 Datacontext 类中有一个 DateTime 项目:
I have an DateTime item in the Datacontext class:
private DateTime _DueDate;
public DateTime DueDate
{
get { return this._DueDate; }
set
{
if (this._DueDate != value)
{
this._DueDate = value;
base.PropertyOnChanged("DueDate");
}
}
}
和 DateTimeConverter:
And the DateTimeConverter:
public class DateConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
DateTime date = ((DateTime)value);
return date.Day + "." + date.Month + "." + date.Year;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
return DateTime.Parse((string)value);
}
}
这也是日历视图的文档:
Here is also the Doc to the Calender View:
在文档中是一个属性 SelectedDate,但我只在 XAML SelectedDateChanged EventHandler 中看到.但我想在 MVVM 中做到这一点.
In the Docs is a Property SelectedDate, but I only see in the XAML SelectedDateChanged EventHandler. But I want to do it in MVVM.
我的问题是我不知道我可以在哪个属性上设置捆绑.我查看了文档,但只找到了 Date=""
属性来自 DatePicker,但我在 CalenderView 中找不到任何内容.
My Problem is I don´t know on which Property I can set the
Binding. I looked in the Doc but I only find the Date=""
property
from the DatePicker but I don´t find anything to the CalenderView.
更新
关注来自
@Juo Zuo:"CalendarView 有一个 SelectedDates 属性.通常,我们可以使用这个属性来设置选定的日期,例如:MyCalendarView.SelectedDates.Add(new DateTime(2016, 5, 5));.但是这个属性是只读,我们不能用它来绑定.所以,恐怕没有办法用绑定设置选定的日期"
@Juo Zuo:"CalendarView has a SelectedDates property. Usually, we can use this property to set the selected date like: MyCalendarView.SelectedDates.Add(new DateTime(2016, 5, 5));. However this property is read-only, we can't use it for binding. So, I'm afraid there is no way to set selected dates with Binding"
我会扩展问题.
我的问题是:
有什么办法可以使用来自 MSDN 的 MVVM 模式的日历视图吗?
Is there any way to use the Calender View with the MVVM Pattern from MSDN ?
推荐答案
您需要做的就是创建一个附加属性并将SelectedDates.Add
逻辑封装在其中
All you need to do is to create an attached property and encapsulate the SelectedDates.Add
logic within it.
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);
}
}
}));
}
<CalendarView local:CalendarViewHelper.SelectedDates="{x:Bind Dates, Mode=OneWay}" />
如果您的 Dates
属性包含多个项目,请确保将 SelectionMode
更改为 Multiple
.
If your Dates
property has more than one items inside, make sure you change the SelectionMode
to Multiple
.
这篇关于如何在 UWP MVVM 中使用 CalenderView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!