在WP10的Pivot中设置并滚动到列表框的SelectedIndex
本文关键字:列表 滚动 SelectedIndex 设置 WP10 Pivot | 更新日期: 2023-09-27 18:10:07
我试图在Pivot中绑定ListBox
的SelectedIndex
。头,项绑定正确,然而,ListBox
SelectedIndex
不知何故不工作。
XAML
<Page.DataContext>
<local:ChapterMenuViewModel/>
</Page.DataContext>
<Pivot x:Name="pvMain" TitleTemplate="{StaticResource PivotTitleTemplate}"
HeaderTemplate="{StaticResource PivotHeaderTemplate}"
ItemsSource="{Binding ChapterMenuHeader}" SelectionChanged="pvMain_SelectionChanged">
<Pivot.ItemTemplate>
<DataTemplate>
<Grid Grid.Row="1" Grid.Column="0">
<ListBox FlowDirection="RightToLeft" FontFamily="./Fonts/ScheherazadeRegOT.ttf#Scheherazade"
x:Name="lsbChapter" ItemTemplate="{StaticResource ChapterItemTemplate}"
SelectedIndex="{Binding SelectedChapterIndex}"
ItemsSource="{Binding Chapters}">
</ListBox>
</Grid>
</DataTemplate>
</Pivot.ItemTemplate>
</Pivot>
MVVM
public class ChapterMenuViewModel : INotifyPropertyChanged
{
ObservableCollection<ChapterMenusHeader> _chapterMenuHeader;
DataSource ds = null;
public ChapterMenuViewModel()
{
ChapterMenuHeader = new ObservableCollection<ChapterMenusHeader>();
ds = new DataSource();
List<JuzDetail> allJuz = DataSource.GetAllJuz;
ChapterMenuHeader.Add(new ChapterMenusHeader() { Header = "chapter", Chapters = DataSource.GetAllChapter, Juzs = allJuz });
ChapterMenuHeader.Add(new ChapterMenusHeader() { Header = "location", Chapters = DataSource.GetAllChapterSortedByChapterType, Juzs = allJuz });
ChapterMenuHeader.Add(new ChapterMenusHeader() { Header = "order", Chapters = DataSource.GetAllChapterSortedByOrder, Juzs = allJuz });
ChapterMenuHeader.Add(new ChapterMenusHeader() { Header = "size", Chapters = DataSource.GetAllChapterSortedBySize, Juzs = allJuz });
ChapterMenuHeader.Add(new ChapterMenusHeader() { Header = "arabic name", Chapters = DataSource.GetAllChapterSortedByArabicAlphabet, Juzs = allJuz });
ChapterMenuHeader.Add(new ChapterMenusHeader() { Header = "english name", Chapters = DataSource.GetAllChapterSortedByEnglishAlphabet, Juzs = allJuz });
}
public ObservableCollection<ChapterMenusHeader> ChapterMenuHeader
{
get { return _chapterMenuHeader; }
set
{
if (_chapterMenuHeader != value)
{
_chapterMenuHeader = value;
OnPropertyChanged("ChapterMenuHeader");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public class ChapterMenusHeader : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public ChapterMenusHeader()
{
SelectedChapterIndex = App.Recent.ChapterID;
}
string _header;
public string Header
{
get { return _header; }
set
{
if (_header != value)
{
_header = value;
OnPropertyChanged("Header");
}
}
}
List<Chapter> _allChapters;
public List<Chapter> Chapters
{
get { return _allChapters; }
set
{
if (_allChapters != value)
{
_allChapters = value;
OnPropertyChanged("Chapters");
}
}
}
int _selectedChapterIndex;
public int SelectedChapterIndex
{
get { return _selectedChapterIndex; }
set
{
_selectedChapterIndex = value;
OnPropertyChanged("SelectedChapterIndex");
}
}
List<JuzDetail> allJuz;
public List<JuzDetail> Juzs
{
get { return allJuz; }
set
{
if (allJuz != value)
{
allJuz = value;
OnPropertyChanged("Juzs");
}
}
}
}
滚动部分private void lsbChapter_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox lsb = sender as ListBox;
if (lsb.SelectedIndex != -1)
scrollIntoSelectedItem(lsb, lsb.SelectedIndex);
}
void scrollIntoSelectedItem(ListBox lsb, int index)
{
lsb.SelectionChanged -= lsbChapter_SelectionChanged;
lsb.SelectedIndex = lsb.Items.Count - 1;
lsb.UpdateLayout();
lsb.ScrollIntoView(lsb.SelectedIndex);
lsb.SelectedIndex = index;
lsb.UpdateLayout();
lsb.ScrollIntoView(index);
lsb.SelectionChanged += lsbChapter_SelectionChanged;
}
这只是我绑定ListBox
的SelectedIndex
的ViewModel
类的一部分。ListBox
项绑定正确,但是SelectedIndex
不工作。如何设置和滚动ListBox
的SelectedIndex
枢轴?
谢谢!
问题出在这一行-
SelectedIndex="{Binding SelectedChapterIndex}" ItemsSource="{Binding Chapters}"
您需要将ItemsSource
绑定移动到SelectedIndex
绑定的前面-
ItemsSource="{Binding Chapters}" SelectedIndex="{Binding SelectedChapterIndex}"
的原因吗?我怀疑这是因为这两个值的实例化顺序。当在xaml中将SelectedIndex
放在ItemsSource
前面并在构造函数中赋值时,ItemsSource
仍然是null
,因此不会选择任何内容。
现在,要滚动到特定项,需要调用ListBox
上的ScrollIntoView
方法。在你的例子中,应该是
lsbChapter.ScrollIntoView(Chapters[SelectedChapterIndex]);