无尽的枢轴控制 [英] Endless Pivot Control
问题描述
我正在尝试将 Pivot
控件用于日历类型的应用程序,其中每个 Pivot
视图显示有关当天的一些信息.当用户向前滑动时,将显示第二天.我通过将项目添加到 Pivot
项目集合的末尾来实现这一点,效果很好.
I'm trying to use a Pivot
control for a calendar type app, where each Pivot
view shows some infos about the current day. When the user swipes forward, the next day is shown. I implemented this by adding items to the end of the Pivot
Item collection, which works fine.
当用户尝试返回到前一天时,就会出现我的问题.在这种情况下,将在 Pivot
项目集合的开头添加一个新项目.尽管添加有效,但显示的 Pivot 项目始终是错误的(即新添加的项目).在 Pivot
控件上设置 SelectedItem
没有帮助.
My problem occurs when the user tries to go backward to the previous day. In this case a new item is added at the beginning of the Pivot
item collection. Although the adding works, the shown Pivot item is always the wrong one (ie. the newly added item). Setting SelectedItem
on the Pivot
control doesn't help.
我认为 Pivot
可能不是我的任务的正确控件,因此非常感谢有关使用什么视图或如何解决上述 Pivot
问题的任何帮助.
I think Pivot
might not be the right control for my task, so any help about what view to use or how fix my aforementioned problem with Pivot
are highly appreciated.
我的 Viewmodel 的代码,它在某一天实现前进/后退.Pages
绑定到 Pivot ItemSource
.
code for my Viewmodel that implements going forward/backward one day.
Pages
is bound to the 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)));
}
}
}
};
}
}
解决我的问题的更改:
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));
}
}
};
推荐答案
为此,我将使用 4 页的 Pivot 控件.
For this, I'd use a Pivot control with 4 pages.
在任何时候,上一页、当前页和下一页都将包含正确的数据 - 您将始终拥有一个(空)页面
At any one time, the previous, current and next pages will contain correct data - and you will always have one (empty) page
然后,您可以在当前页面更改和更改时响应事件 - 使用这些事件将当前(空)页面设置为正确的新内容,然后清除新(空)页面.
You can then respond to the events when your current page is changing and has changed - use these events to set up the current (empty) page to the correct new content and to then clear the new (empty) page.
这篇关于无尽的枢轴控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!