MVVMLightを使用してアプリケーションにMVVMパターンを適用しようとしています。私はデータバインドされたリストボックスを持っています...
MainView.xaml[抽出]
<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" >
MainViewModel.cs[抽出]
private Recipe selectedRecipe;
public Recipe SelectedRecipe
{
get
{
return selectedRecipe;
}
set
{
selectedRecipe = value;
RaisePropertyChanged("SelectedRecipe");
}
}
... SelectionChangedでページナビゲーションを実行します:
MainView.xaml.cs[抽出]
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に設定されます!新しいビューに移動し、選択したアイテムのプロパティを表示し、戻ったときに選択したインデックスをリセットするにはどうすればよいですか?ありがとうございました!