Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silverlight: "The name already exists in the tree"

this is a problem that regularly arises when I write Silverlight XAML. In this case, I've made a usercontrol VerticalTabStop (code attached) that has a ToolTip attached. I instanciate a couple of my usercontrols, and then I get the debugging window and the following error:

Line:52
Error: Unhandled Error in Silverlight 2 Application
Code: 2028
Category: ParserError
Message: The name already exists in the tree: AltLabel.
File:
Line: 0
Position: 0

I get an awful lot of these messages as I hover my mouse over the buttons. Any suggestions to what I'm doing wrong here?

Cheers

Nik


<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    x:Class="SLEntityPresenterWebPart.VerticalTabStop"
    d:DesignWidth="20" d:DesignHeight="27">

    <Grid x:Name="LayoutRoot">
        <StackPanel>
            <Canvas x:Name="TabStopCanvas" Height="27" Width="20">
                <ToolTipService.ToolTip>
                    <TextBlock x:Name="AltLabel" Text="Substitute me"/>
                </ToolTipService.ToolTip>
                <Image x:Name="IconImg" Canvas.Left="7" Canvas.Top="9" Width="26" Height="26" Source="Contact.png" Canvas.ZIndex="5" Margin="0,-9,0,0" RenderTransformOrigin="0.5,0.5">
                    <Image.RenderTransform>
                        <TransformGroup>
                            <ScaleTransform ScaleX="0.85" ScaleY="0.85"/>
                            <SkewTransform/>
                            <RotateTransform/>
                            <TranslateTransform X="0"/>
                        </TransformGroup>
                    </Image.RenderTransform>
                </Image>
                <Image Source="stop.png" Margin="3,0,0,0"/>
            </Canvas>

        </StackPanel>
    </Grid>
</UserControl>
like image 385
niklassaers-vc Avatar asked Dec 03 '22 16:12

niklassaers-vc


2 Answers

There is very similar bug even in Silverlight 4. If you create custom usercontrol, usually:

<UserControl xmlns:MyNameSpace="clr-namespace:MyNameSpace" x:Class="MyNameSpace.MyClass" 
x:Name="userControl" ... />

Then, if you add 2 controls without names to the xaml code (with preview):

<MyNameSpace:MyClass ... />
<MyNameSpace:MyClass ... />

There will be exception "The name already exists in the tree: userControl". It occurs because Silverlight can't find the name (unnamed [MyClass]) and looks to the UserControl where it finds "userControl" twice.

One of the solution is to give some names to the controls:

<MyNameSpace:MyClass x:Name = "MyControl1" ... />

Or initialize this control from code:

MyClass control = new MyClass();
SomeGrid.Children.Add(control);
like image 97
Artur A Avatar answered Dec 27 '22 01:12

Artur A


This is a bug in Silvelight. The way to work around it is to remove the Name attribute on the TextBlock in the Tooltip.

I presume that you have the name there for a reason, and that not being able to refer to this element from code is going to be a problem for you. As a work around for that, try replacing the tooltip xaml with this:

<ToolTipService.ToolTip>
    <ToolTip x:Name="AltLabel" Content="Substitute me" />
</ToolTipService.ToolTip>

Now you can get to the text by doing AltLabel.Content. If this does not solve your problem, please let me know.

like image 41
KeithMahoney Avatar answered Dec 27 '22 00:12

KeithMahoney