Select ListBox Item on the Hold event in Windows Phone

I was working on an application and I had to select a List Box Item on the hold event. My List Box was as below,

image

You see in the List Box, I have put an Image control and binding Source and Tag dependency property of the image to the data source.

At the code behind, I got an entity class Photo as below,

image

Finally, I am setting ItemsSource of ListBox as below. lstPhotos is list of Photo.

image

Now question comes that how to set a particular image from list of images in ListBox on the hold event. When user hold a particular image in ListBox that image should get selected. For that I have raised a hold event on the Image control in List Box and in the hold event need to fetch the selected image as data context.

image

In above code you cans see that I am getting data context of selected image in instance of entity class photo. In this way you can select a ListBox item on the hold event.

Before I conclude one point keep in mind that you need to raise Hold event on the container inside the ListBox. For example, if you have a StackPanel inside DataTemplate then you need to raise the Hold event on StackPanel. In my case there is no container but an Image so I am raising Hold event on the Image control. Avoid raising hold event on ListBox itself else you will get NullReference exception.

I hope this quick post is useful. Thanks for reading.

Dhananjay Kumar is Developer, Blogger , Speaker, Learner , Mindcracker & Microsoft MVP.

Tagged with: , ,
Posted in Windows Phone Dev
4 comments on “Select ListBox Item on the Hold event in Windows Phone

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

  • I just want to create an App not a rocket science program .. why this separate VM this and that for WP8 emulator ? Big fail #Fail 1 hour ago
  • seems whole world is conspiring against my webinar tomorrow .. This is last thing I epxpected now why you are not working ? :( 4 hours ago
  • what is this security group and all ? being fan of WP only question why so many complexities product group ? 4 hours ago
  • dude I tell you this Windows Phone 8 SDK and emulator is pain ... u crash systems ? 4 hours ago
  • RT @brij_bhushan: #Blogged WCF Hands on Lab – Csharp Corner Delhi Chapter event : A Very Successful day wp.me/pT2AF-jc cc @debug_mo4 hours ago
Categories
Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my current or previous employer's view in anyway. © Copyright 2013
Follow

Get every new post delivered to your Inbox.

Join 2,133 other followers

%d bloggers like this: