Silverlight ListBox SelectedItem binded:页面导航,获取项目,在新视图中显示其属性,并重置SelectedIndex。我该怎么办?
我正试图用MVVM Light在我的应用程序中应用MVVM模式。我有一个数据索引列表框 MainView.xaml[摘录]Silverlight ListBox SelectedItem binded:页面导航,获取项目,在新视图中显示其属性,并重置SelectedIndex。我该怎么办?,silverlight,windows-phone-7,mvvm,listbox,databound,Silverlight,Windows Phone 7,Mvvm,Listbox,Databound,我正试图用MVVM Light在我的应用程序中应用MVVM模式。我有一个数据索引列表框 MainView.xaml[摘录] <ListBox Name="recipesListBox" ItemsSource="{Binding RecipeList}" SelectedItem="{Binding SelectedRecipe, Mode=TwoWay}"
<ListBox Name="recipesListBox"
ItemsSource="{Binding RecipeList}"
SelectedItem="{Binding SelectedRecipe, Mode=TwoWay}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Grid.Row="1"
Margin="12,0,12,0"
SelectionChanged="recipesListBox_SelectionChanged" >
private Recipe selectedRecipe;
public Recipe SelectedRecipe
{
get
{
return selectedRecipe;
}
set
{
selectedRecipe = value;
RaisePropertyChanged("SelectedRecipe");
}
}
private void recipesListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string destination = "/RecipeView.xaml";
if (recipesListBox.SelectedIndex == -1) // If selected index is -1 (no selection) do nothing
return;
this.NavigationService.Navigate(new Uri(destination, UriKind.Relative));
//recipesListBox.SelectedIndex = -1; // Reset selected index to -1 (no selection)
}
…对SelectionChanged执行页面导航:
MainView.xaml.cs[摘录]
<ListBox Name="recipesListBox"
ItemsSource="{Binding RecipeList}"
SelectedItem="{Binding SelectedRecipe, Mode=TwoWay}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Grid.Row="1"
Margin="12,0,12,0"
SelectionChanged="recipesListBox_SelectionChanged" >
private Recipe selectedRecipe;
public Recipe SelectedRecipe
{
get
{
return selectedRecipe;
}
set
{
selectedRecipe = value;
RaisePropertyChanged("SelectedRecipe");
}
}
private void recipesListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string destination = "/RecipeView.xaml";
if (recipesListBox.SelectedIndex == -1) // If selected index is -1 (no selection) do nothing
return;
this.NavigationService.Navigate(new Uri(destination, UriKind.Relative));
//recipesListBox.SelectedIndex = -1; // Reset selected index to -1 (no selection)
}
在//之后,您可以看到在页面导航之后重置索引的旧代码-现在,使用绑定数据,显然它还将null设置为my selected item!返回时,如何导航到新视图、显示选定项的属性并重置选定索引?谢谢大家! 最好的方法是将您的收藏存储在您可以随时随地访问它的地方(如在Windows Phone Databound应用程序项目中),并在导航uri中传递所选项目的索引
int index = recipesListBox.SelectedIndex;
this.NavigationService.Navigate(new Uri(destination +"?index="+index , UriKind.Relative));
然后在新页面的OnNavigatedTo方法中,从查询中获取索引并获取项目
override OnNavigatedTo(
{
string index;
if(NavigationContext.QueryString.TryGetValue("index", index))
{
// get the item from your collection based on this index
}
}
我是这样做的:
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
if (listBox.SelectedItem != null)
{
listBox.SelectedIndex = -1;
}
base.OnNavigatedFrom(e);
}
然后将以下内容添加到SelectionChanged事件
if(SelectedItem != null)
{
// Do something with SelectedItem
}
谢谢你的回答!我以前的代码是这样的:Recipe selectedRecipe=Recipes ListBox.SelectedItem作为Recipe;this.NavigationService.Navigate(新Uri(destination+“?RecipeID=“+selectedRecipe.ID,UriKind.Relative));this.recipesListBox.SelectedIndex=-1。。。在OnNavigatedTo中,我通过ID在xml“数据库”上使用LINQ查询检索了配方,只要我在方法中执行操作,所有操作都正常工作,但在方法之外,配方显示为空!很明显,我保存了“外部”配方,但它总是显示为空!通常在OnNavigatedTo中,在outside中为null:(您是说查询字符串“RecipeID”不可用且为空?应保留在NavigationContext中。或者您是说配方本身为空?或者配方集合为空?在哪个页面中为空?配方本身为空!我在新视图中的代码如下:private Recipe selectedRecipe;override on NavigatedTo({string ID;if(NavigationContext.QueryString.TryGetValue(“ID”,ID)){this.selectedRecipe=GetRecipeByID(ID);}}}即,如果我尝试获取名称(selectedRecipe.Name)在OnNavigatedTo内部,我得到了我想要的东西,如果我在外部尝试,我得到了NullsomethingException!感谢您的回答!我昨天自己找到了解决方案,我完全按照您的方式编写了代码!除了if()和base。OnNavigatedFrom(e),我现在正在添加它XD再次感谢您