Microsoft.Samples.KMoore.WPFSamples.DateControls.MonthCalendar.SetSelectionByRange C# (CSharp) Method

SetSelectionByRange() private method

Select the dates between start and end. If one of them is null, only select the other one
private SetSelectionByRange ( System.DateTime start, System.DateTime end ) : void
start System.DateTime
end System.DateTime
return void
        private void SetSelectionByRange(DateTime? start, DateTime? end)
        {
            if (SelectedDates.Count == MaxSelectionCount)
            {
                return;
            }

            if (!start.HasValue)
            {
                SelectedDate = end;
            }
            else if (!end.HasValue)
            {
                SelectedDate = start;
            }
            else
            {
                SelectionChange.Begin();
                bool succeeded = false;
                try
                {
                    foreach (DateTime dt in SelectedDates)
                    {
                        if (!MonthCalendarHelper.IsWithinRange(dt, start.Value, end.Value))
                        {
                            SelectionChange.Unselect(dt);
                        }
                    }

                    DateTime date = start.Value;
                    while (date <= end.Value && SelectedDates.Count < MaxSelectionCount)
                    {
                        if (!SelectedDates.Contains(date))
                        {
                            SelectionChange.Select(date, true);
                        }
                        date = date.AddDays(1);
                    }

                    SelectionChange.End(true, true);
                    succeeded = true;
                }
                finally
                {
                    if (!succeeded)
                    {
                        SelectionChange.Cancel();
                    }
                }
            }
        }