UWP:在日历视图中绑定所选日期

时间:2017-05-02 15:45:47

标签: c# xaml mvvm uwp bind

我想在我的XAML日历视图中从我的ViewModel到选定日期(单个)进行双向绑定。 但是我们没有依赖属性来做它。

怎么办?

编辑: 我试过的代码,但我得到了错误。 它结合了这两个主题: 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}"
                          />

我的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;}

帮助者:

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

}

1 个答案:

答案 0 :(得分:3)

您需要创建DateTimeOffset的附加属性,而不是IList<DateTimeOffset>,因为您只想选择一个日期。

因此,您需要这样的代码 -

,而不是您从我的其他答案中复制的代码
public static class CalendarViewHelper
{
    public static DateTimeOffset GetSelectedDate(DependencyObject obj)
    {
        return (DateTimeOffset)obj.GetValue(SelectedDateProperty);
    }

    public static void SetSelectedDate(DependencyObject obj, DateTimeOffset value)
    {
        obj.SetValue(SelectedDateProperty, value);
    }

    public static readonly DependencyProperty SelectedDateProperty =
        DependencyProperty.RegisterAttached("SelectedDate", typeof(DateTimeOffset), typeof(CalendarView),
            new PropertyMetadata(null, (d, e) =>
            {
                var cv = (CalendarView)d;
                var date = (DateTimeOffset)e.NewValue;

                cv.SelectedDates.Clear();
                cv.SelectedDates.Add(date);
            }));
}

请注意,您需要调用Clear来清除所有以前选择的日期,因为SelectionMode设置为Single,如果不这样做,则会引发错误。

然后,您只需要在xaml中将其与SelectedDate属性挂钩。

<CalendarView SelectionMode="Single"
                  DisplayMode="Month"
                  local:CalendarViewHelper.SelectedDate="{x:Bind SelectedDate, Mode=OneWay}" />