Error while creating SubMenus via Templating

Mar 29, 2011 at 2:33 PM


I tried to create SubMenus via templating in Silverlight:

The Error occurs if I add an PopupMenu directly inside a PopupMenuItem. Same error if the PopupMenu is empty:

    <DataTemplate x:Key="MenubarSubItemTemplate">
        <my:PopupMenuItem ToolTipService.ToolTip="{Binding Tooltip}" Header="{Binding Caption}" ImageRightSource="Resources/Images/arrow.png">
                <ListBox Style="{StaticResource PopupMenuStyle}" ItemTemplate="{StaticResource MenubarItemBaseTemplate}" ItemsSource="{Binding MenuItems}" />

The error message looks like this:

 Error: Unhandled Error in Silverlight Application: The object reference is not set to an instance of an object.
   at SL4PopupMenu.PopupMenuBase.triggerElement_LayoutUpdated(Object sender, EventArgs e)
   at System.Windows.FrameworkElement.OnLayoutUpdated(Object sender, EventArgs e)
   at MS.Internal.JoltHelper.RaiseEvent(IntPtr target, UInt32 eventId, IntPtr coreEventArgs, UInt32 eventArgsTypeIndex)

 I want to create the same SubMenus like this example:

Mar 29, 2011 at 3:08 PM

The reason of that error was that the 'sender' in the 'triggerElement_LayoutUpdated' was 'null'

I've comment out a few lines and it works now for me (PopUpMenuBase.cs; Line 483):

//if (triggerElement.Parent == null)
//    triggerElement.LayoutUpdated += triggerElement_LayoutUpdated;
    triggerElement_LayoutUpdated(triggerElement, null);

Do you know the reason why the 'sender' is 'null', if I want to use templates?

Apr 8, 2011 at 9:08 AM
Edited Apr 8, 2011 at 9:15 AM

Hi Juergengutsch,

A bit of research lead me to the MSDN docs which states that the LayoutUpdated event always has a null sender. This wasn't expected at all and might look like a bug, but it is actually by design!

Thanks for reporting this and providing the workaround for it as well. Lessons learnt...I'll entirely rely on the dispatcher object instead for such purposes from now on.

You can expect this to be fixed in the next code release due next week.