ContextMenu - Build Dynamically SubMenu - AG_E_UNKNOWN_ERROR [Line: 1 Position: 1449]

4 posts, 1 answers
  1. Pinho
    Pinho avatar
    42 posts
    Member since:
    Mar 2009

    Posted 14 Jun 2009 Link to this post

    Hi,

     I've got a sub menu (inside a context menu) that is being built dinamically.
     If I've the context menu open or it was open before, and if I try to clear all menu items of the sub menu or try to add a new sub menu, I get an exception:

     AG_E_UNKNOWN_ERROR [Line: 1 Position: 1449]

    I define a context menu and I set it to open with:

    contextMenu.EventName = "MouseLeftButtonDown";




    It's defined for a image:

    <Image x:Name="imgSearch" HorizontalAlignment="Center" Source="/AssemblyName;component/Images/selection-arrow-16x16.png" Height="16" Width="16" Margin="0,2,2,2" Cursor="Hand"
                    <telerikNavigation:RadContextMenu.ContextMenu> 
                        <telerikNavigation:RadContextMenu x:Name="contextMenu" ModifierKey="None" Background="White" Opened="contextMenu_Opened"
                            <telerikNavigation:RadMenuItem Header="Find" Click="RadMenuItem_Click"
                                <telerikNavigation:RadMenuItem.Icon> 
                                    <Image Width="16" Height="16" Source="/AssemblyName;component/Images/Magnifier-16x16.png"/> 
                                </telerikNavigation:RadMenuItem.Icon> 
                            </telerikNavigation:RadMenuItem> 
                            <telerikNavigation:RadMenuItem Header="Simple Query" Click="RadMenuItem_Click"
                                <telerikNavigation:RadMenuItem.Icon> 
                                    <Image Width="16" Height="16" Source="/AssemblyName;component/Images/Help-16x16.png" /> 
                                </telerikNavigation:RadMenuItem.Icon> 
                            </telerikNavigation:RadMenuItem> 
                            <telerikNavigation:RadMenuItem x:Name="radMenuItemSavedQueries" Header="Stored Queries" Background="White"
                                <telerikNavigation:RadMenuItem.Icon> 
                                    <Image Width="16" Height="16" Source="/AssemblyName;component/Images/save-blue-16x16.png" /> 
                                </telerikNavigation:RadMenuItem.Icon> 
                            </telerikNavigation:RadMenuItem> 
                        </telerikNavigation:RadContextMenu> 
                    </telerikNavigation:RadContextMenu.ContextMenu> 
                </Image> 


    I go to a db get some values to fill the submenu that I fill in a callback (async. get):

    RadMenuItem menuItem = new RadMenuItem(); 
    foreach (Object result in results) 
        menuItem = new RadMenuItem() { Header = result.Name, Name = result.Name }; 
        menuItem.Click += new RoutedEventHandler(RadMenuItem_Click); 
        try 
        { 
            radMenuItemSavedQueries.Items.Add(menuItem); 
        } 
        catch (Exception ex) 
        { 
          Console.Write(ex.Message); 
        } 

    Is there anything I am doing wrong? How should this be done?

    Thanks for the help,

    L. Pinho


  2. Answer
    Valeri Hristov
    Admin
    Valeri Hristov avatar
    2252 posts

    Posted 15 Jun 2009 Link to this post

    Hi Luis,

    The easiest workaround for this problem is to avoid clearing all MenuItem sub-items when the MenuItem has an image:
    // Store the count of the original items in order to be able to remove them later
    int originalItemsCount = radMenuItemSavedQueries.Items.Count;

    // Add the new items here
    RadMenuItem menuItem = new RadMenuItem();
    foreach (Object result in results)

        menuItem = new RadMenuItem() { Header = result.Name, Name = result.Name }; 
        menuItem.Click += new RoutedEventHandler(RadMenuItem_Click); 
        try 
        { 
            radMenuItemSavedQueries.Items.Add(menuItem); 
        } 
        catch (Exception ex) 
        { 
            Console.Write(ex.Message); 
        }
    }

    // Remove the original items
    for (int i=0; i<originalItemsCount; i++)
    {
        radMenuItemSavedQueries.Items.RemoveAt(0);
    }

    The problem is related to a bug in the Silverlight 2 Control class, which does not clear the TemplateBinding in the control template when it is changed. Since RadMenuItem changes its template depending on the count of its items - when there are no items a different template is used, there is a moment where the MenuItem Icon becomes with two parents, hence the exception.

    As far as I know, in Silverlight 3 this bug is fixed, but we will look for a workaround in our controls in case there is strong demand for Silverlight 2 versions after the Silverlight 3 release.

    Regards,
    Valeri Hristov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. DevCraft banner
  4. Pinho
    Pinho avatar
    42 posts
    Member since:
    Mar 2009

    Posted 15 Jun 2009 Link to this post

    Hi Valeri,

     when I do the 1st add, the exception is throwned, from what I can see, if I open the context menu once, before being fully build, when I try to add the sub menu is throws an exception.

     My workaround was to add a item in xaml (Please wait... Menu being built) and then remove this extra item as you state in your code.

    I think this might be an issue, but your workaround helped me enough, thanks

    L. Pinho
  5. Jamest
    Jamest avatar
    41 posts
    Member since:
    Apr 2007

    Posted 22 Sep 2009 Link to this post

    Just an FYI to this thread, I am using SL3 and I am still experiencing this issue with an exception thrown when dynamically clearing/adding sub items when the root menu item has an icon set in xaml. I removed the icon definition from xaml and set it the icon in code and the exception is no longer thrown.
Back to Top