Search code examples
c#windows-phone-7onclickstackpanel

Doing onclick on stackpanel (Window Phone 7.0)


How can I do a onclick on the stackpanel inside a list ?? I have a list of data that is retrieve from the WCF service. My current codes are below:

        <ListBox x:Name="Results" Height="450" HorizontalAlignment="Left" VerticalAlignment="Bottom" ItemsSource="{Binding Items}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="20,0,0,20"  >
                       <TextBlock Text="{Binding SearchVal}"  TextWrapping="Wrap" FontSize="{StaticResource PhoneFontSizeMediumLarge}" />
                       <TextBlock Text="{Binding Category}"  TextWrapping="Wrap" FontSize="{StaticResource PhoneFontSizeMedium}" />
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

I have went to a lot website to research, I tired mouse cannot, Tap, Up a lot more..

I have used the codes you provided, but inside the ItemTapped, I have no button, so I change to button to the TextBlock. But it wont work.

//I try changing button to TextBlock, wont work.
var selectedSearchBtn = sender as Button; //Assuming your trigger is a button UI element. 

        if (selectedSearchBtn != null)
        {
            var selectedVal = selectedSearchBtn.DataContext as TestMap.Classes.Global.Place;
            Classes.Global.searchedValue = "Test to get into the loop in main";

            if (selectedVal != null)
            {
                Classes.Global.posy = selectedVal.Y;
                Classes.Global.posx = selectedVal.X;

            }

            NavigationService.GoBack();
        }

Solution

  • See sample here

    <ListBox x:Name="Results" Height="450" HorizontalAlignment="Left" VerticalAlignment="Bottom" ItemsSource="{Binding Items}">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <StackPanel Margin="20,0,0,20" Tap="ItemTapped">
                                        <TextBlock Text="{Binding SearchVal}"  TextWrapping="Wrap" FontSize="{StaticResource PhoneFontSizeMediumLarge}" />
                                        <TextBlock Text="{Binding Category}"  TextWrapping="Wrap" FontSize="{StaticResource PhoneFontSizeMedium}" />
                                    </StackPanel>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
    
    private void ItemTapped(object sender, GestureEventArgs e)
        {
            var element = sender as FrameworkElement; //
    
            if (element != null)
            {
                var selectedVal = element.DataContext as TestMap.Classes.Global.Place;
                Classes.Global.searchedValue = "Test to get into the loop in main";
    
                if (selectedVal != null)
                {
                    Classes.Global.posy = selectedVal.Y;
                    Classes.Global.posx = selectedVal.X;
    
                }
    
                NavigationService.GoBack();
            }
        }