- I have a parent VisualElement, “Parent”, that when clicked, hits a callback
- I have a child VisualElement of “Parent”, “Child”, that when clicked, hits a callback
Is it possible to not trigger the parent VisualElement callback, when the specific “Child” is clicked (even though it is nested in the parent)?
Have you tried calling StopPropagation() on the event in the child callback? That should be enough to prevent the event from reaching the parent.
2 Likes
I will try this out thank you!
I tried this and received an error: The name “StopPropagation” does not exist in the current context
On what object are you trying to call StopPropagation() on? You have to do it on the event received on the callback. If you register a callback for ClickEvent for example, you’d do this:
// Callback registration
parent.RegisterCallback<ClickEvent>(ParentCallback);
child.RegisterCallback<ClickEvent>(ChildCallback);
// Callback definitions
void ParentCallback(ClickEvent evt)
{
Debug.Log("Parent Callback called");
}
void ChildCallback(ClickEvent evt)
{
Debug.Log("Child Callback called");
// Remove this next line and the Parent Callback will be called:
evt.StopPropagation();
}