Пример #1
0
        private void PeriodListSnapped_ItemClick(object sender, ItemClickEventArgs e)
        {
            try
            {
                DateTime nav = ((DateTime)((e.ClickedItem as Viewbox).DataContext));
                CalculateDateDifference dateDiff;

                // if the month has NOT already been loaded, load into memory
                if (!isMonthLoaded(nav.Year, nav.Month))
                {
                    // since this is specific to months in the future we only need to see
                    // where it is relative to our last month
                    DateTime last = new DateTime(_allMonths.Last().Year, _allMonths.Last().Month, 1);

                    // get the difference
                    dateDiff = new CalculateDateDifference(last, nav);

                    // now add all the months to the flipview
                    for (int i = 0; i < dateDiff.Months; i++)
                    {
                        last = last.AddMonths(1);
                        AddMonthToCal(last.Year, last.Month);
                    }
                }

                // now that it's loaded, navigate to it

                // first determine which month we're at
                CalendarMonth curMonth = (CalendarFlip.SelectedItem as CalendarMonth);
                DateTime      curDate  = new DateTime(curMonth.Year, curMonth.Month, 1);

                // then determine how many months we need to move (not specific to forward or back)
                dateDiff = new CalculateDateDifference(curDate, nav);

                // then determine if the current month is ahead or behind where we need to go
                int compare = curMonth.Compare(nav.Year, nav.Month);

                // if we're trying to navigate to the current month we're at... gtfo
                if (compare == 0)
                {
                    return;
                }
                else if (compare > 0) // the current month is further than where we want to nav... go back
                {
                    CalendarFlip.SelectedIndex -= dateDiff.Months;
                }
                else // the navigation point is further than where we're at... go forward
                {
                    CalendarFlip.SelectedIndex += dateDiff.Months;
                }
            }
            catch { }
        }
Пример #2
0
        /// <summary>
        /// Add a single month to the UI and underlying calendar
        /// </summary>
        /// <param name="Year">The current Year as int</param>
        /// <param name="Month">The current Month as int</param>
        /// <returns>True if the add was successfull, false if the month already existed in the list</returns>
        private CalendarMonth AddMonthToCal(int Year, int Month)
        {
            bool          toFront     = false;
            CalendarMonth returnMonth = null;

            // determine if this month should go in the front or back
            // we always assume the caller has given us either the next
            // or previous month... we do no validation
            if (_allMonths == null || _allMonths.Count == 0)
            {
                toFront = false;
            }
            else
            {
                CalendarMonth first = _allMonths[0];

                if (first.Compare(Year, Month) > 0)
                {
                    toFront = true;
                }
                else
                {
                    toFront = false;
                }
            }

            if (toFront)
            {
                // figure out how much time from today to this value "in the past"
                TimeSpan ts = DateTime.Today - new DateTime(Year, Month, 1);

                // if it's too far back don't add it
                if (Math.Abs(ts.Days) > Period.DAYS_IN_YEAR * Period.YEARS_BACK)
                {
                    return(returnMonth);
                }
            }
            else
            {
                // figure out how much time from today to this value "in the past"
                TimeSpan ts = DateTime.Today - new DateTime(Year, Month, 1);

                // if it's too far back don't add it
                if (Math.Abs(ts.Days) > Period.DAYS_IN_YEAR * Period.YEARS_FORWARD)
                {
                    return(returnMonth);
                }
            }


            // if this month isn't already loaded, add it
            if (!isMonthLoaded(Year, Month))
            {
                CalendarMonth cm = new CalendarMonth(Year, Month);

                // ensure the proper colors are added to it with the
                // most recent period
                if (mostRecentPeriod != null)
                {
                    Add_Period_To_Calendar(mostRecentPeriod, cm);
                }

                // ensure the calendar is appropriate to the current
                // view mode (snapped vs normal)
                if (App.isSnapped())
                {
                    cm.SnapView();
                }

                if (toFront)
                {
                    _allMonths.Insert(0, cm);
                }
                else
                {
                    _allMonths.Add(cm);
                }

                _changingSelection       = true;
                CalendarFlip.ItemsSource = _allMonths;
                _changingSelection       = false;

                returnMonth = cm;
            }

            return(returnMonth);
        }