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.