是的,所以我是这样实现的:
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
ViewModel ViewModel { get { return this.DataContext as ViewModel; } }
private void Button_Click_1(object sender, RoutedEventArgs e)
{
this.ViewModel.MovePrevious();
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{
this.ViewModel.MoveNext();
}
}
public class ViewModel : INotifyPropertyChanged
{
public ViewModel() { SelectedThing = ThingList.First(); }
List<int> m_ThingList = Enumerable.Range(1, 50).ToList();
public List<int> ThingList { get { return m_ThingList; } set { SetProperty(ref m_ThingList, value); } }
int m_SelectedThing = default(int);
public int SelectedThing { get { return m_SelectedThing; } set { SetProperty(ref m_SelectedThing, value); } }
internal void MovePrevious()
{
var _CurrentIndex = ThingList.IndexOf(this.SelectedThing);
try { SelectedThing = ThingList[--_CurrentIndex]; }
catch { SelectedThing = ThingList.First(); }
}
internal void MoveNext()
{
var _CurrentIndex = ThingList.IndexOf(this.SelectedThing);
try { SelectedThing = ThingList[++_CurrentIndex]; }
catch { SelectedThing = ThingList.Last(); }
}
public event PropertyChangedEventHandler PropertyChanged;
void SetProperty<T>(ref T storage, T value, [System.Runtime.CompilerServices.CallerMemberName] String propertyName = null)
{
if (!object.Equals(storage, value))
{
storage = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
和
<Page.DataContext>
<local:ViewModel />
</Page.DataContext>
<Page.BottomAppBar>
<AppBar>
<Grid>
<Button HorizontalAlignment="Left" Click="Button_Click_1">Previous</Button>
<Button HorizontalAlignment="Right" Click="Button_Click_2">Next</Button>
</Grid>
</AppBar>
</Page.BottomAppBar>
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<FlipView x:Name="MyFlip"
ItemsSource="{Binding ThingList}"
SelectedItem="{Binding SelectedThing,
Mode=TwoWay}" />
</Grid>
嗯,它工作正常。你能仔细检查一下吗?或者提供更多信息?否则,也许使用我的指导更新您的实施可以为您解决这个问题。但愿如此。
实际上,我会使用 DelegateCommand 来处理按钮。但是我不想用太多的样板代码来过度使用这个例子。如果你想看看我会做什么,你可以看这里:http ://blog.jerrynixon.com/2012/08/most-people-are-doing-mvvm-all-wrong.html