Endless turn

I am trying to use a control Pivotfor a calendar type application, where each Pivotview shows some information about the current day. When the user goes forward, the next day is displayed. I implemented this by adding items to the end of the item collection Pivot, which works great.

My problem occurs when the user tries to return to the previous day. In this case, a new item is added at the beginning of the item collection Pivot. Although adding works, the displayed Pivot element is always incorrect (i.e., the newly added element). Setting SelectedItemin the control Pivotdoes not help.

I think that Pivotmay not be the right control for my task, so any help regarding what kind to use or how my aforementioned problem is fixed with Pivotis highly appreciated.

for my Viewmodel model that implements forward / backward movement one day. Pagesattached to Pivot ItemSource.

public class TrackDayViewModel : HubViewModelBase
{
    private DateTime _CurrentDay;
    public DateTime CurrentDay
    {
        get { return _CurrentDay; }
        set
        {
            if (value.CompareTo (_CurrentDay) != 0)
            {
                _CurrentDay = value;
                OnPropertyChanged("CurrentDay");
            }
        }
    }

    public TrackDayViewModel ()
    {
        var day = DateTime.Now;

        CurrentDay = day.Midnight();

        Pages.Add(new DayViewModel(CurrentDay.AddDays(-1)));
        Pages.Add(new DayViewModel(CurrentDay));
        Pages.Add(new DayViewModel(CurrentDay.AddDays(1)));

        SelectedItem = Pages[1];

        this.PropertyChanged += (s, e) =>
        {
            if (e.PropertyName == "SelectedItem")
            {
                var si = SelectedItem as DayViewModel;

                if (si != null)
                {
                    var idx = Pages.IndexOf(SelectedItem);
                    if (idx==0)
                    {
                        Pages.Insert(0, new DayViewModel(si.Day.AddDays(-1)));
                        SelectedItem = Pages[1];
                    }
                    else if (idx == (Pages.Count - 1))
                    {
                        Pages.Add(new DayViewModel(si.Day.AddDays(1)));
                    }
                }
            }
        };
    }
}

EDIT: change what solved my problem:

        this.PropertyChanged += (s, e) =>
        {
            if (e.PropertyName == "SelectedItem")
            {
                var si = SelectedItem as DayViewModel;

                if (si != null)
                {
                    var idx = Pages.IndexOf(SelectedItem);

                    int nextIdx = (idx + 1) % 3;
                    int prevIdx = ((idx - 1)<0)  ? 2 : (idx-1);

                    Pages[nextIdx] = new DayViewModel(si.Day.AddDays(1));
                    Pages[prevIdx] = new DayViewModel(si.Day.AddDays(-1));
                }
            }
        };
+5
source share
2 answers

For this, I would use a 4-page Pivot element.

At any time, the previous, current and next pages will contain the correct data - and you will always have one (blank) page

, , - , () () .

+5

Pivot , , 6 .

.

TransitioningContentControl . TransitioningContentControl .

+1

All Articles