Every ItemsControl has an ItemsContainerGenerator, which (surprisingly enough) is responsible for generating a control for each item in the list. It provides a few useful methods for finding the container of a given item, and vice versa. You can use it like this.
Up vote 2 down vote favorite share g+ share fb share tw.
In a continuation from my question here I tried using the solution provided in the linked answer, however when I give it the image which the ListBox is binding to, it gives me the wrong position, because the Item Template is loading in a source URL rather than the actual image, and it seems the data is null. My Item Template looks like this: How do I get access to "ListImage" for the currently selected Item? C# windows-phone-7 xaml windows-phone-7.1 windows-phone-7.5 link|improve this question asked Feb 8 at 8:57Bob3938 50% accept rate.
– Erno Feb 8 at 9:20 @Erno I want to find it's global position on the phone's screen, as related to my linked question. – Bob Feb 8 at 9:33.
Every ItemsControl has an ItemsContainerGenerator, which (surprisingly enough) is responsible for generating a control for each item in the list. It provides a few useful methods for finding the container of a given item, and vice versa. You can use it like this: private void listBoxSource_SelectionChanged(object sender, SelectionChangedEventArgs e) { var listBox = (ListBox) sender; var containerGenerator = listBox.
ItemContainerGenerator; var container = (UIElement)containerGenerator. ContainerFromItem(listBox. SelectedItem); } You can then use the variable container with the solution from your other post to find the coordinates, Point position = container.
GetRelativePosition(Application.Current. RootVisual); And on a side note, in your DataTemplate you don't need the StackPanel, since the ListBox is providing that with its ItemsPanelTemplate.
Trying this now, thanks. It seemed to work, but for some reason it's telling me that the position of the Image is -352 when its in the middle of the screen. Any ideas whats going on there?
– Bob Feb 8 at 10:54 Nevermind, the guy in the other answer gave me the input backwards :) Thanks a ton for this! Wish I could give an extra +1 – Bob Feb 8 at 11:02 1 Yeah, it was backwards. I'll fix my answer (and maybe simplify it a bit).
– ChimeraObscura Feb 8 at 11:10.
On tap event you can do this: private void image_Tap(object sender, System.Windows.Input. GestureEventArgs e) { Image selectedImage = e. OriginalSource as Image; } is this what you need?
Thanks I'll try that and get back here. – Bob Feb 8 at 10:27 That almost seemed to work, but adding a tap event to the image seems to cause the selectedItem event to not work properly. I think they run in the wrong order to be useful.
– Bob Feb 8 at 10:45 as I see you found your answer hope I could help. Cheers. – SmaartMobile Feb 8 at 11:22 1 I gave it an upvote, it was most certainly helpful.
Thank you. – Bob Feb 8 at 11:35.
I would suggest a different solution because the way you are trying to solve it (finding the control) is prone to error. Make the DataTemplate a resource and refer to it in the ListBox by using ItemTemplate="{StaticResource myTemplate}" Next, when an item is selected, add a ContentControl to the Canvas, set its ContentTemplate to the same DataTemplate and the DataContext to the SelectedItem. This way you only have to define the template once (one place to maintain it) and do not have to walk the VisualTree.
– Bob Feb 8 at 10:29 It will not give you the position of the rendered item. If you want to put something on top just put the Image in the template in a Grid, add yet another image in the same grid and set its Source to a secondImageUrl. Then, when an Item is selected set the secondImageUrl property of the selectedItem.
Try to stay away from manipulating the UI, use binding instead. – Erno Feb 8 at 10:34 Thanks, I think this will help me make my app better, but for this specific piece of it, I need an item duplicated in the same spot that it's been selected so it can be dragged around and played with by the user. – Bob Feb 8 at 10:55.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.