Answered by:
Reference Elements by String

Question
-
I have Image1,Image2,Image3 all handled by one event handler for Tapped.
Now I need to make it so at the end of the slideshow it displays a message to the user, I have a grid for each set that's not visible...all named, EndOfImageSet1, EndOfImageSet2, and EndOfImageSet3.
I know how to get the number from the end of the Image1,Image2,Image3 from the sender variable on the event handler, and go through all the pictures, detect when the slideshow is at an end and should display the grid notifying the user the end of the slideshow is there, but how do I reference EndOfImageSetX using a string by using "EndOfImageSet" + CType(sender,Image).Name.Last which would be a string of EndOfImageSet + the last character which is the number of the image set.
So how would I reference the control like this to set visibility? Currently I use "If ReferenceEquals(sender,Image1)" and one for each image then to make the EndOfImageSetX visible for each separately...I was hoping to reduce the code by specifically referencing the other control from a string.
- Edited by Maya Key Sunday, January 4, 2015 2:43 PM
Sunday, January 4, 2015 2:42 PM
Answers
-
Hi Michael,
I am not sure if I understand you properly. You put three grids to display three sets of images, and now you want to use one method to handle end of image sets event. Use FrameworkElement.FindName method to get the controls in XAML and then get more properties. http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.frameworkelement.findname.aspx.
If this doesn’t help, please post more information or some code snippets to explain your scenario.
Regards,
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.
- Marked as answer by Maya Key Monday, January 5, 2015 5:16 PM
Monday, January 5, 2015 6:24 AMModerator
All replies
-
Hi Michael,
I am not sure if I understand you properly. You put three grids to display three sets of images, and now you want to use one method to handle end of image sets event. Use FrameworkElement.FindName method to get the controls in XAML and then get more properties. http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.frameworkelement.findname.aspx.
If this doesn’t help, please post more information or some code snippets to explain your scenario.
Regards,
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.
- Marked as answer by Maya Key Monday, January 5, 2015 5:16 PM
Monday, January 5, 2015 6:24 AMModerator -
Hi Michael,
You could use the following recursive function to find an element in the visual tree based on its type and name. It takes the parent container of the visual element and the name of the element as parameters:
Private Shared Function GetChild(Of t As DependencyObject)(ByVal parent As DependencyObject, ByVal name As String) If parent Is Nothing Then Return Nothing Dim foundChild As t = Nothing Dim childrenCount = VisualTreeHelper.GetChildrenCount(parent) For i As Integer = 0 To childrenCount - 1 Dim child = VisualTreeHelper.GetChild(parent, i) Dim childType As t = TryCast(child, t) If (childType Is Nothing) Then foundChild = GetChild(Of t)(child, name) If (foundChild IsNot Nothing) Then Exit For ElseIf Not String.IsNullOrEmpty(name) Then Dim fe As FrameworkElement = TryCast(child, FrameworkElement) If (fe IsNot Nothing And fe.Name = name) Then foundChild = child Exit For End If Else foundChild = child Exit For End If Next Return foundChild End Function
For example, if you want to get a reference to an Image named "Image1" that is located somewhere on the Page (Me / this) in which the GetChild method above is implemeted, you would use the following code:Dim img As Image = GetChild(Of Image)(Me, "Image1")
<Image x:Name="Image1" />
Hope that helps.
Please remember to mark helpful posts as answer and/or helpful.
Monday, January 5, 2015 2:28 PM -
The FindName method worked great for my use case, I reduced 9 lines of code to 1 line of code...All I needed was to use FindName(), CType() the object returned to an image, and then I could access all the variables and everything as I needed to show the correct image accordingly without having to type out lines of code to handle each of the images separately considering it does the same action across all of those images handled.Monday, January 5, 2015 5:23 PM