Click here to Skip to main content
15,891,513 members
Articles / All Topics
Technical Blog

Handle OnClick Event in ListBox on Silverlight for Windows Phone 7

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
22 Jul 2013CPOL 6.2K   1  
Handle OnClick event in ListBox on Silverlight for Windows Phone 7.

Generally on Windows Phone 7 using Silverlight You will use ListBox templates like this one

XML
<ListBox Margin="0,0,-12,0" ItemsSource="{Binding Items}" >  
    <ListBox.ItemTemplate>  
        <DataTemplate>  
            <StackPanel Orientation="Horizontal" Margin="0,0,0,17"  
                       MouseLeftButtonDown="StackPanel_MouseLeftButtonDown">  
                <Image Height="150" Width="150" 
                       Source="{Binding ImageSource}" Stretch="UniformToFill" />  
                <StackPanel Width="311">  
                    <TextBlock Text="{Binding LineOne}" 
                        TextWrapping="Wrap" 
                        Style="{StaticResource PhoneTextExtraLargeStyle}"/>  
                    <TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" 
                        Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>  
                </StackPanel>  
            </StackPanel>  
        </DataTemplate>  
    </ListBox.ItemTemplate>  
</ListBox>

But on touch based platform such as Windows Phone 7, MouseLeftButtonDown will trigger event, even if You try scroll ListBox content. Another approach is to use ManipulationCompleted event. Event arguments have property named IsTapEvent, but in latest Silverlight SDK this property is declared as Protected. So You can see it in debugger at runtime, but cannot use in code.

What You can do is subscribe to ManipulationCompleted event, and check e.TotalManipulation for Scale and Transform properties equals zero.

C#
private void StackPanel_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)    
{    
    var zero = new Point(0,0);    
    if( e.TotalManipulation.Scale == zero && e.TotalManipulation.Translation == zero )    
        MessageBox.Show("gotcha!");    
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) i-BLADES
Thailand Thailand
I'm Android and Full Stack Software Engineer. 28 years in software industry, lots of finished projects. I’m never afraid of learning something new and you can see it by amount of skills in my resume.

I'm working remotely since 2009, self-motivated and self-organized.

There are no impossible projects. I have experience with Android, iOS, Web, Desktop, Embedded applications, VR, AR, XR, Computer vision, Neural networks, Games, IoT, you name it.

Comments and Discussions

 
-- There are no messages in this forum --