![](/img/trans.png)
[英]Cannot set Opacity property on ScatterViewItem after an animation has been performed
[英]Set the SelectedIndex property of a ListView just after it has been populated
我想將ListView的SelectedIndex屬性設置為在終止應用程序之前選擇的值。
因此,在暫停應用程序之前,我保存了SelectedIndex值。
現在在OnLaunched()方法中,當我將SelectedIndex屬性設置為該值時,我得到了ArgumentException,該異常表示值不在預期范圍內。 我對其進行搜索,發現ListView的SelectedIndex屬性在填充之前已更改(因此,那時SelectedIndex的唯一有效值為-1)。
好的。 但是如何填充ListView之后如何設置SelectedIndex屬性?
最簡單的方法是什么? 我應該深入研究ItemsChanged事件嗎?
更新:這是代碼:
public sealed partial class MyPage : Page
{
public List<String> myList { get; set; }
public MyPage()
{
this.InitializeComponent();
myList = new List<string>()
{
"hello",
"this",
"is",
"me"
}; //This is the ItemSource for the ListView
...
}
public void SetUpUI(int selectedItem) //This method is called from
{ //the OnLaunched() method
MyListView.SelectedIndex = selectedItem;
}
...
}
使用Page.Load
或Page.LoadComplete
事件設置您選擇的項目。
通過使用此頁面的Loaded事件解決了此問題,如下所示:
public sealed partial class MyPage : Page
{
public List<String> myList { get; set; }
int selectedItem = -1;
public MyPage()
{
this.InitializeComponent();
myList = new List<string>()
{
"hello",
"this",
"is",
"me"
};
Application.Current.Suspending += App_Suspending;
this.Loaded += MyPage_Loaded;
}
private void MyPage_Loaded(object sender, RoutedEventArgs e)
{
MyListView.SelectedIndex = selectedItem;
}
...
public void SetUpUI(int prevSelectedIndex)
{
selectedItem = prevSelectedIndex;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.