Nathan
Nathan

Reputation: 2513

Navigate to page based on itemview clicked item

i have a simple list that is bound to a gridview control, upon the itemview's click event i'd like to navigate to the page.

My class looks like;

public class GetMenu
    {
        public string titleName { get; set; }
        public string imagePath { get; set; }
        public string pagePath { get; set; }
    }

An example of the data with the list;

new GetMenu(){titleName = "Services", imagePath = "Bouquets.xaml", pagePath="Services.xaml"}

For the click even have the following;

void ItemView_ItemClick(object sender, ItemClickEventArgs e)
    {
    }

I believe i need to extract the click event data from e, i'm a little unsure on how to do this.

Upvotes: 1

Views: 1069

Answers (1)

Patryk Ćwiek
Patryk Ćwiek

Reputation: 14318

If I understand your question correctly, i.e you want to get 'clicked' item, then it should be fairly easy:

var getMenu = (GetMenu)e.ClickedItem;

Now you have the item and you can use the properties inside as navigation parameters.

Is that what you had in mind?

[EDIT]

The navigation itself is fairly simple, too. If you're in code-behind, you have to:

Frame.Navigate(typeof(YourViewForTheItem), parameters);

e.g.

Frame.Navigate(typeof(ItemDetailsView), getMenu);

parameters is an object, so you will have to cast it appropriately in OnNavigatedTo in the target view.

If you're using any kind of MVVM framework, there are services for that too, e.g. Caliburn.Micro has INavigationService.

That's of course if you know the type beforehand.

If you want to create the 'type' itself from a string you have, you will have to use reflection:

var viewType = Type.GetType("YourStoreApp.Views."+getMenu.pagePath.Substring(0, getMenu.pagePath.LastIndexOf("."));

Assuming the pagePath is not null.

The type string has to be fully qualified name, that is full assembly name and type (without extension), so e.g "YourStoreApp.Views.Services". The file name has to mirror the type name exactly for this to work though.

Now you can:

Frame.Navigate(viewType);

Upvotes: 1

Related Questions