hxdai
hxdai

Reputation: 53

How to set event for a control in a datatemplate used by a longlistselector

I'm developing a windows phone 8 app, and I'm having trouble handling events when I tap an item in a longlistselector.

So I have this data template:

<DataTemplate x:Key="InfoDataTemplate">
    <Grid Margin="12,0,12,0" Width="auto">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Image x:Name="UserAvatar" Margin="0,12,0,0" Grid.Row="0" Grid.Column="0" Grid.RowSpan="3" Source="{Binding user.avatar_url}" VerticalAlignment="Top"/>
        <TextBlock x:Name="Username" Margin="12,0,0,0" Text="{Binding user.username}" Grid.Row="0" Grid.Column="1" FontSize="24" FontWeight="Bold"/>

I put this data template as part of in app.xaml since I reuse this in different pages.

I'm using this data template in a longlistselector in a page(page1.xaml):

<phone:LongListSelector x:Name="UserList" ItemTemplate="{StaticResource InfoDataTemplate}" SelectionChanged="List_SelectionChanged" ItemRealized="List_ItemRealized">                        

The intended behavior is that when I tap the image in the template I navigate to page A, when I tap anywhere else in the data template I navigate to page B. Is this possible? If so how should I implement it? Thanks

Upvotes: 1

Views: 474

Answers (1)

Manvik
Manvik

Reputation: 977

It is possible to implement this behavior. You need to do the following

  • Add a tap event for Image, say ImageTapped
  • Add a tap event for the LonglistSelector ListItemTapped

Now when you click on the image it will call the ImageTapped event first (you need to set a flag here) and then it calls the ListItemTapped event (you can check if the flag is set) and then act upon accordingly. Don't forget to reset the flag in the ListItemTapped event.

Upvotes: 1

Related Questions