This is a migrated thread and some comments may be shown as answers.

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

3 Answers 137 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Pinho
Top achievements
Rank 1
Pinho asked on 14 Jun 2009, 03:47 PM
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


3 Answers, 1 is accepted

Sort by
0
Accepted
Valeri Hristov
Telerik team
answered on 15 Jun 2009, 09:07 AM
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.
0
Pinho
Top achievements
Rank 1
answered on 15 Jun 2009, 01:24 PM
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
0
Jamest
Top achievements
Rank 2
answered on 22 Sep 2009, 09:46 PM
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.
Tags
Menu
Asked by
Pinho
Top achievements
Rank 1
Answers by
Valeri Hristov
Telerik team
Pinho
Top achievements
Rank 1
Jamest
Top achievements
Rank 2
Share this question
or