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,


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,


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


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.


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.

Published by

Dhananjay Kumar

Dhananjay Kumar is Developer Evangelist for Infragistics. He is a 8 times Microsoft MVP and well respected Developer Advocate in India.He is the author of 900+ Blog Posts, and can often be found speaking around India at conferences and hosting free workshops for programmers across the country. So far, he has hosted 60 free workshops on various topics like JavaScript, Angular, WCF, ASP.NET MVC, C#, Azure etc. Follow him on twitter @debug_mode for all the updates about his blog posts and workshops. You can send him email at debugmode [at] outlook [dot] com

5 thoughts on “Select ListBox Item on the Hold event in Windows Phone”

  1. Pingback:

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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