Jonnybomb
Jonnybomb

Reputation: 3

Windows Phone 8.1 listview click to change page

For school i have to make a Windows Phone 8.1 program (in MVVM style) but i’m stucked.

I have a list of cocktails and I get them in a listview, dynamically with bindings:

<ListView ItemsSource="{Binding Cocktails}" ItemTemplate="{StaticResource AllCocktailsTemplate}" > </ListView>

It looks like that: http://hpics.li/08e9e96

It works perfectly but what I want is when I click on a cocktail, I change the page to go to the page of this cocktail.

The navigation works but I don’t know how to do it on the listview for each cocktails (which are get from a DB)

I’m on it from yesterday morning, I found nothing useful for me on internet so I hope you could help me :p

If you need more informations, just ask me

Jonny

Upvotes: 0

Views: 2056

Answers (1)

Nguyen Kien
Nguyen Kien

Reputation: 1927

ListView/GridView have ItemClick event, just subscribe to it

Xaml:

<ListView ItemClick="OnPostItemClick" IsItemClickEnabled="True">

Code:

private void OnPostItemClick(object sender, ItemClickEventArgs e)
{
    // Navigate to cocktail page with item you click/tap on
    Frame.Navigate(typeof(YourPage), e.ClickedItem);
}

If you want use Command with ItemClick event you need add Behaviors SDK, then use this way

<Page ...
    xmlns:i="using:Microsoft.Xaml.Interactivity"
    xmlns:c="using:Microsoft.Xaml.Interactions.Core">

<ListView>
    <i:Interaction.Behaviors>
        <c:EventTriggerBehavior EventName="ItemClick">
            <c:InvokeCommandAction Command="{Binding Path=YourCommand}"/>
        </c:EventTriggerBehavior>
    </i:Interaction.Behaviors>
</ListView>

Upvotes: 4

Related Questions