Name Already exists in the tree

5 posts, 0 answers
  1. om
    om avatar
    26 posts
    Member since:
    Jan 2009

    Posted 02 Feb 2009 Link to this post

    Hi,

    I created a basic user control with RadMenu in it:
    <UserControl x:Class="MenuTest.menu" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
        xmlns:radMenu="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation" 
          
        Width="400" Height="300">  
        <Grid x:Name="LayoutRoot" Background="White">  
            <radMenu:RadMenu x:Name="rad1" > 
                <radMenu:RadMenuItem x:Name="mainitem1" Header="main"/>  
                <radMenu:RadMenuItem x:Name="mainitem2" Header="main2">  
                    <radMenu:RadMenuItem x:Name="subitem1" Header="sub1" /> 
                    <radMenu:RadMenuItem x:Name="subitem2" Header="sub2" /> 
                </radMenu:RadMenuItem> 
            </radMenu:RadMenu> 
        </Grid> 
    </UserControl> 

    Then I placed this user control in a page twice:
    <UserControl x:Class="MenuTest.Page" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
        xmlns:local="clr-namespace:MenuTest" 
        Width="400" Height="300">  
        <Grid x:Name="LayoutRoot" Background="White">  
            <Grid.RowDefinitions> 
                <RowDefinition Height="20"/>  
                <RowDefinition Height="20"/>  
            </Grid.RowDefinitions> 
            <Grid.ColumnDefinitions> 
                <ColumnDefinition Width="200"/>  
                <ColumnDefinition Width="200"/>  
            </Grid.ColumnDefinitions> 
            <local:menu x:Name="test" Grid.Row="0"></local:menu> 
            <local:menu x:Name="test1" Grid.Row="1"></local:menu> 
        </Grid> 
    </UserControl> 

    When I executed the page and hovered over the menu "test", everything went fine, until I hovered over the second menu "test1". which gave me the error: "The Name Already Exists in the Tree: subItem1.. MethodName: "

    It actually started happening in the application we are really working on. In there we have a page which has RadMenu. If we open the instance of that page for the first time, everything goes fine. But either if we close the opened page and reopen it and try to hover over the menu, it gives us the same problem. Similarly, if we had open two instances of the page which has RadMenu, the first page works all fine but on the second page if you try to access the menu you get the same error.

    BUT, this problem disappears if we stop using "x:name" for menu and the items for the menu. Seems like this menu has some static property which causes the conflict.

    I would love to know your thoughts about this, because to me it seems like a bug, I might be wrong though.

    Regards,
    OM
  2. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 03 Feb 2009 Link to this post

    Hello om,

    This is the expected behavior. You cannot have a two elements with the same Name (or x:Name) on them. Try it with Buttons instead of RadMenu. Each defined must be unique within a XAML namescope.
    You can read more here:
    http://msdn.microsoft.com/en-us/library/cc189028(VS.95).aspx

    Let me know if you need more information.

    Greetings,
    Hristo
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. DevCraft banner
  4. om
    om avatar
    26 posts
    Member since:
    Jan 2009

    Posted 03 Feb 2009 Link to this post

    Hi Hristo,
    Thanks alot for your reply and I tried it with button instead of rad menu as you suggested, like:

    <UserControl x:Class="MenuTest.menu" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   
        xmlns:radMenu="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation" 
          
        Width="400" Height="300">  
        <Grid x:Name="LayoutRoot" Background="White">  
            <!--<radMenu:RadMenu x:Name="rad1" > 
                <radMenu:RadMenuItem x:Name="mainitem1" Header="main"/>  
                <radMenu:RadMenuItem x:Name="mainitem2" Header="main2">  
                    <radMenu:RadMenuItem x:Name="subitem1" Header="sub1" /> 
                    <radMenu:RadMenuItem x:Name="subitem2" Header="sub2" /> 
                </radMenu:RadMenuItem> 
            </radMenu:RadMenu>--> 
            <Button x:Name="btnTest" Click="btnTest_Click" ></Button>  
        </Grid> 
    </UserControl> 

    and it works all fine, if it is the namescope why doesn't it apply on button and even more important fact is that if you remove x:Name attribute from RadMenuItem(s) only then problem disappears. It seems like there is something shared (static) inside RadMenuItem which is causing this problem. What do you think about it?

    Regards
    OM
  5. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 03 Feb 2009 Link to this post

    Hello om,

    I'm sorry for misleading you. Every UserControl is new namescope. So in theory you shouldn't experience this error. But as you know RadMenu uses Popup control to open its subitems. And as it turns out the if popup contains ItemsControl and the items of this itemsControl have Name (as your case) then an exception is raised.  This has nothing to do with RadMenu implementation (no static/shared references).

    You can try this scenario with this simple UserControl:
    <UserControl x:Class="SilverlightApplication19.SilverlightControl1" 
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="400" Height="300">  
        <Grid x:Name="LayoutRoot" Background="White">  
            <Popup x:Name="popup">  
                <Border BorderBrush="Black" BorderThickness="10">  
                    <ItemsControl> 
                        <ContentControl x:Name="content" 
                                Content="I'm in Popup/ItemsControl.Items and have a x:Name='content'" /> 
                    </ItemsControl> 
                </Border> 
            </Popup> 
            <Button x:Name="btn" VerticalAlignment="Center" Content="Open/Close popup" 
                    Click="Button_Click" /> 
        </Grid> 
    </UserControl> 

    and this is the code behind:
    using System.Windows;  
    using System.Windows.Controls;  
     
    namespace SilverlightApplication19  
    {  
        public partial class SilverlightControl1 : UserControl  
        {  
            public SilverlightControl1()  
            {  
                InitializeComponent();  
            }  
     
            private void Button_Click(object sender, RoutedEventArgs e)  
            {  
                popup.IsOpen = !popup.IsOpen;  
            }  
        }  

    Here you won't see custom controls but the problem still exist.
    I'm sorry for the inconvenience. I hope that Microsoft will fix this for Silverlight 3.

    Kind regards,
    Hristo
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  6. om
    om avatar
    26 posts
    Member since:
    Jan 2009

    Posted 03 Feb 2009 Link to this post

    Thanks for your reply Hristo!
Back to Top
DevCraft banner