Dodsworth
asked on
Calling an Instance of a Custom Control
I have this piece of code that creates a sentence using CustomControls...
The CustomControl shows a single letter and has some animations built into it. So the code above splits a sentence into words and then creates a CustomControl for each letter in a word and adds it to a Horizontal StackPanel, so that complete words wrap correctly in a WrapPanel.
The animations were triggered from mouse/touch events in the custom control, but now I need to trigger them through code outside the CustomControl.
So if the original sentence was "Hello World", I need to be able to get a handle to say, letter(3) in the sentence and trigger the animation for the CustomControl that represents that letter.
Hope that makes (some) sense.
Private Sub buildSentence(sentence As String)
Dim words As String() = sentence.Split(New Char() {" "c})
For Each word In words
Dim sp As New StackPanel
sp.Margin = New Thickness(0, 0, 16, 0)
sp.Orientation = Controls.Orientation.Horizontal
sp.HorizontalAlignment = System.Windows.HorizontalAlignment.Center
Dim letters As Char() = word.ToCharArray
For Each letter In letters
Dim lctl As New LetterCustomControl
lctl.Text = letter
sp.Children.Add(lctl)
Next
wrapPanel.Children.Add(sp)
Next
End Sub
The CustomControl shows a single letter and has some animations built into it. So the code above splits a sentence into words and then creates a CustomControl for each letter in a word and adds it to a Horizontal StackPanel, so that complete words wrap correctly in a WrapPanel.
The animations were triggered from mouse/touch events in the custom control, but now I need to trigger them through code outside the CustomControl.
So if the original sentence was "Hello World", I need to be able to get a handle to say, letter(3) in the sentence and trigger the animation for the CustomControl that represents that letter.
Hope that makes (some) sense.
ASKER
Forget about how to trigger the event.. I can do that fine once I have a reference to the correct control.
'sp' is garbage after each word is rendered, so I can use it.
'sp' is garbage after each word is rendered, so I can use it.
A quick code to reference the correct control could be:
wrapPanel.Children(0).Chil dren(2) would be to access (word #1).(letter #3)
because the wrapPanel instance has StackPanel instances and each StackPanel instance has LetterCustomControl instances.
Note: For your final implementation you may consider having a GetAssociateLetterCustomCo ntrol function in your custom control to provide some encapsulation to avoid accessing the Children property directly.
wrapPanel.Children(0).Chil
because the wrapPanel instance has StackPanel instances and each StackPanel instance has LetterCustomControl instances.
Note: For your final implementation you may consider having a GetAssociateLetterCustomCo
ASKER
I had to break you idea down into two casts..
I'm assuming that the compiler wouldn't have known that the contents of the wrapPanel where StackPanels.
How would I go about writing GetAssociateLetterCustomCo ntrol ?
Dim stack As StackPanel = TryCast(wrapPanel.Children(1), StackPanel)
Dim lctl As LetterCustomControl = TryCast(stack.Children(1), LetterCustomControl)
I'm assuming that the compiler wouldn't have known that the contents of the wrapPanel where StackPanels.
How would I go about writing GetAssociateLetterCustomCo
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
sp.children(2) would be letter #3
As to triggering something we'd need more details on the control. However, if you can call the event and pass it (nothing, nothing) that will fake the call, probably.