Could not find any element named XXX for trigger type RightClick in the visual tree.

Dec 15, 2010 at 4:01 PM
Edited Dec 15, 2010 at 7:07 PM


I use this menu in any places on my App. But in one place I have this error

Could not find any element named btnBusq for trigger type RightClick in the visual tree.

The source code point that throw this exception is PopUpMenuUtils.cs and the procedure name is FindApplicationElementByName

This is the snapshot of the error:

public static FrameworkElement FindApplicationElementByName(string elementName, string elementQualifierForErrorMsg)
			object obj = (Application.Current.RootVisual as FrameworkElement).FindName(elementName.Trim());
			if (obj == null) // Object not found. Use the more thorough but also more costly method.
				obj = Application.Current.RootVisual.GetVisualDescendants()
					.Where(elem => elem.Name == elementName).FirstOrDefault();

			if (obj != null)
				if (obj is UIElement)
					return obj as FrameworkElement;
					if (elementQualifierForErrorMsg != null && !DesignerProperties.IsInDesignTool) // Error messages are disabled at design time
						throw new ArgumentException("The " + elementQualifierForErrorMsg + " is referenced to " + elementName + " which is not a UIElement.");
				if (elementQualifierForErrorMsg != null && !DesignerProperties.IsInDesignTool) // Error messages are disabled at design time
					throw new ArgumentException("Could not find any element named " + elementName + " for " + elementQualifierForErrorMsg + " in the visual tree.");
			return null;

btnBusq Sure exist

 I have a track: I use XAML file for define the instance of menu. I suspicious that when this procedure is executed the event <Loaded> of the host control is not created, and the element isnt exist on stack returns by GetVisualDescendants. If you create the menu from behind code on loaded event instead XAML file, works fine because at moment of system fire the event <loaded> the stack of GetVisualDescendants then contains btnBusq.

This code works fine, btnBusq is now "Button1"

    Private Sub UCtrlBuscador_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Dim sp As New StackPanel
        sp.Children.Add(New TextBlock With {.Text = "Busquedas"})

        Dim myItemMenu1 As New SL4PopupMenu.PopupMenuItem
        myItemMenu1.Header = "Option1"

        Dim myItemMenu2 As New SL4PopupMenu.PopupMenuItem
        myItemMenu2.Header = "Option2"

        Dim mymenu As New SL4PopupMenu.PopupMenu
        mymenu.AddTrigger(SL4PopupMenu.TriggerTypes.LeftClick, Me.Button1)
        mymenu.AddTrigger(SL4PopupMenu.TriggerTypes.RightClick, Me.Button1)


        Me.Button1.Content = sp

Mar 7, 2011 at 3:07 AM

Glad you found the workaround when you stumble on this type of issue.

You can also put the PopupMenu inside Button1 and set the value of both LeftClickElements and RightClickElements to {Parent}.

This will tell the menu to use its own parent as trigger element and should end up the same. This approach has the advantage of not using code behind.

However please make sure you are using the latest bits from here before trying it: