Search code examples
c#windows-phone-7listpicker

How to refresh the current page after selecting an item in ListPicker


I'd like to know how to refresh the current page with

heNavigationService.Navigate(new Uri(NavigationService.Source + "?Refresh=true", UriKind.Relative));

after i pick an element in the ListPicker.


Solution

  • I suppose you are using MVVM Light for Windows Phone. In that case, you should catch the event in your page and then trigger a command on your ViewModel.

    Example:

    Code-behind of the page

    private void Listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        ViewModelClass vm = this.DataContext as ViewMoedlClass;
        if (vm != null)
        {
            vm.RefreshCommand.Execute();
        }
    }
    

    ViewModel

    class ViewModelClass
    {
        public ViewModelClass
        {
            this.RefreshCommand = new RelayCommand(() =>
            {
                NavigationService.Navigate(new Uri(NavigationService.Source + "?Refresh=true", UriKind.Relative));
            }   
        }
    
        public RelayCommand RefreshCommand { get; set;}
    
    }
    

    Xaml

    <ListBox SelectionChanged="Listbox_SelectionChanged" />
    

    In theory you shouldn't have to do this in your code-behind and you would bind your Command from the ViewModel directly to the SelectionChanged-event, but this is not (directly) possible in Windows Phone. If you want to go this route you can take a look at EventToCommand. This page explains the steps in more detail: http://www.geekchamp.com/articles/how-to-bind-a-windows-phone-control-event-to-a-command-using-mvvm-light