UWP CalendarView 添加 SelectedDays 不会更改可见月份

UWP CalendarView adding SelectedDays doesn't change visible month

我正在使用 CalendarView 和自定义行为 select 整个星期,当用户点击这一天时,它工作正常。但是,如果我 select 这个星期不在当月,天数会突出显示,但我必须手动更改月份 - 视图不会更新并停留在当月......知道如何更改视图以显示月份自动 selected 天数?

WeekHighlightBehavior.cs

public class WeekHighlightBehavior : Behavior
        {
            public CalendarView CalendarControl
            {
                get { return (CalendarView)GetValue(CalendarProperty); }
                set { SetValue(CalendarProperty, value); }
            }

            // Using a DependencyProperty as the backing store for Calendar.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty CalendarProperty =
                DependencyProperty.Register("CalendarControl", typeof(CalendarView), typeof(WeekHighlightBehavior), new PropertyMetadata(0));

            protected override void OnAttached()
            {
                base.OnAttached();
                CalendarControl.SelectedDatesChanged += Calendar_SelectedDatesChanged;
            }

            private void Calendar_SelectedDatesChanged(CalendarView sender, CalendarViewSelectedDatesChangedEventArgs args)
            {
                var dayItem = this.AssociatedObject as CalendarViewDayItem;
                var calendar = CultureInfo.CurrentUICulture.Calendar;
                bool highlight = false;
                if (args.AddedDates != null && args.AddedDates.Count() > 0)
                {
                    foreach (var date in args.AddedDates)
                    {
                        if (calendar.GetWeekOfYear(date.DateTime, CalendarWeekRule.FirstDay, DayOfWeek.Monday) ==
                            calendar.GetWeekOfYear(dayItem.Date.DateTime, CalendarWeekRule.FirstDay, DayOfWeek.Monday))
                        {
                            highlight = true;
                            break;
                        }
                    }
                }

                if (highlight)
                {
                    dayItem.Background = new SolidColorBrush(Color.FromArgb(255, 223, 231, 241));
                }
                else
                {
                    dayItem.Background = null;
                }
            }

            protected override void OnDetaching()
            {
                base.OnDetaching();
                CalendarControl.SelectedDatesChanged -= Calendar_SelectedDatesChanged;
            }
        }

CalendarPage.xaml.cs

private void CalendarViewDayItemChanging(CalendarView sender, CalendarViewDayItemChangingEventArgs args)
                {
                    var behavior = new WeekHighlightBehavior();
                    behavior.CalendarControl = sender;
                    Interaction.GetBehaviors(args.Item).Clear();
                    Interaction.GetBehaviors(args.Item).Add(behavior);
                    var vm = ServiceLocator.Current.GetInstance<SampleViewModel>();
                    if (sender.SelectedDates != null && sender.SelectedDates.Count() > 0)
                    {
                        foreach (var day in sender.SelectedDates)
                        {
                            sender.SelectedDates.Remove(day);
                        }
                    }
                    sender.SelectedDates.Add(vm.SelectedDate);
                }

private void SelectedDateChanged(object sender, CalendarViewSelectedDatesChangedEventArgs e)
                {
                    var vm = ServiceLocator.Current.GetInstance<SampleViewModel>();
                    if (e.AddedDates == null || e.AddedDates.Count() == 0)
                    {
                        return;
                    }
                    vm.SelectedDate = e.AddedDates.FirstOrDefault().DateTime;
                }

CalendarPage.xaml

<CalendarView SelectedDatesChanged="SelectedDateChanged" CalendarItemBorderThickness="0" IsTodayHighlighted="False" CalendarItemBackground="#ffffff" SelectedBorderBrush="#DFE7F1" FirstDayOfWeek="Monday" CalendarViewDayItemChanging="CalendarViewDayItemChanging">
                    <CalendarView.CalendarViewDayItemStyle>
                        <Style TargetType="CalendarViewDayItem">
                            <Setter Property="Padding" Value="0" />
                            <Setter Property="Margin" Value="0" />
                        </Style>
                    </CalendarView.CalendarViewDayItemStyle>
                </CalendarView>

通过将 sender.SetDisplayDate(vm.SelectedDate) 添加到 CalendarLoaded 方法解决了问题,该方法分配给 CalendarView 中的 Loaded 属性 CalendarPage.xaml .

已更新CalendarPage.xaml.cs

private void CalendarViewDayItemChanging(CalendarView sender, CalendarViewDayItemChangingEventArgs args)
    {
        var behavior = new WeekHighlightBehavior();
        behavior.CalendarControl = sender;
        Interaction.GetBehaviors(args.Item).Clear();
        Interaction.GetBehaviors(args.Item).Add(behavior);
    }

private void CalendarLoaded(object sender, RoutedEventArgs args)
    {
        var calendar = sender as CalendarView;
        if (calendar.SelectedDates != null && calendar.SelectedDates.Count() > 0)
        {
            foreach (var day in calendar.SelectedDates)
            {
                calendar.SelectedDates.Remove(day);
            }
        }
        var vm = ServiceLocator.Current.GetInstance<SampleViewModel>();
        calendar.SetDisplayDate(vm.SelectedDate);
        calendar.SelectedDates.Add(vm.SelectedDate);
    }