在FlipView_SelectionChanged事件中设置ListView的SelectedIndex
我正在使用FlipView
构建一个Photo应用程序。 在BottomAppBar中,当我在ListView
单击它时,我放置了所有图像的ListView
,以便能够在FlipView
查看图像,并获得在ListView
选择的FlipView
显示的图像(如分页)。
在listView.selectionChanged
事件中,当我在ListView
选择它时,我创建了在FlipView
中显示图像的代码。 这是代码:
private void listView_SelectionChanged(object sender, SelectionChangedEventArgs e) { string CurrentViewState = ApplicationView.GetForCurrentView().Orientation.ToString(); int IndicatorIndex = listView.SelectedIndex; GoToPage(CurrentViewState, IndicatorIndex); } private void GoToPage(string CurrentViewState, int IndicatorIndex) { if (CurrentViewState == "Portrait") { flipView1.SelectedIndex = IndicatorIndex; } else if (CurrentViewState == "Landscape") { if (IndicatorIndex % 2 == 0) flipView1.SelectedIndex = IndicatorIndex / 2; else { if (IndicatorIndex == 1) flipView1.SelectedIndex = 1; else flipView1.SelectedIndex = (IndicatorIndex + 1) / 2; } } }
现在我需要根据flipView.SelectedIndex
更改listView.SelectedIndex
listView.SelectedIndex = flipView.SelectedIndex
我有一个例外:
An exception of type 'System.ArgumentException' occurred in eBookApp.exe but was not handled in user code. Additional information: Value does not fall within the expected range.
我需要能够在FlipView
中选择相同的图像,选择并在ListView
滚动它…
我最终通过添加到我的FlipView
来使其工作:
SelectedIndex="{Binding Path=SelectedIndex, ElementName=listView1, Mode=TwoWay}"
和我的ListView
:
SelectedIndex="{Binding Path=SelectedIndex, ElementName=flipView1, Mode=TwoWay}"
他们的SelectedIndex
互相引用!
而不是选择更改事件,更简单的方法是使用数据绑定到SelectedIndex。