none
Runtime "Name already exists in tree" error when using multiple instances of a ComboBox

    Question

  •  I created a UserControl to serve as my page, and another UserControl containing a ComboBox which I need to create multiple instances of.

    <UserControl x:Class="MyProject.MyPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:MyProject"
        Width="Auto" Height="Auto">

                    <StackPanel>
                        <local:MyControl x:Name="MyControl_One"/>
                        <local:MyControl x:Name="MyControl_Two"/>
                    </StackPanel>

    </UserControl>

    <UserControl x:Class="MyProject.MyControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Width="Auto" Height="Auto">
        <StackPanel>
            <ComboBox x:Name="MyComboBox">
                <ComboBoxItem x:Name="ItemOne" Content="One"/>
                <ComboBoxItem x:Name="ItemTwo" Content="Two"/>
            </ComboBox>
        </StackPanel>
    </UserControl> 

    The result is:

    Error: Sys.InvalidOperationException: Runtime error 2028 in control 'Xaml1', method: The name already exists in the tree: ItemOne.

    The first UserControl ComboBox works. The second UserControl ComboBox drops down to reveal an empty list.

    This seems to indicate that the two instances of the ComboBox's are attempting to reference the same list of Items.

    I've also encountered this problem with instances that (*should*) have been previously unloaded when I cleared the local reference.

    Note: I can remedy this problem by simply eliminating the names of my ComboBoxItem's or other approaches to populating a ComboBox. I'm not looking for workarounds, just wondering if others can verify the problem or point out my error.

     

     

    Tuesday, February 24, 2009 5:56 PM

Answers

  • Thanks for the report! This is a bug with XAML namescope in this scenario. We have this on our bug list for the next release.

    Thanks, Stefan Wick

    Wednesday, February 25, 2009 12:02 AM

All replies

  • You are trying to put two duplicate ComboBoxItems named "ItemOne" and "ItemTwo" in the StackPanel. You cannot have duplicate names.

    Tuesday, February 24, 2009 6:20 PM
  • And the only reason you are seeing the two ComboBoxes is because you are overriding the x:Name property from "MyComboBox" to "MyControl_X."
    Tuesday, February 24, 2009 6:22 PM
  • I'm adding two UserControls to the StackPanel. Because the ComboBox and ComboBoxItem's are contained within the UserControl, I would have expected a certain level of encapsulation.

    Does Silverlight not use some form of fully qualified names?

    If what you are suggesting is true, I'd have to be careful about using multiple instances of a UserControl. Furthermore, I'd have to be careful to have all my control names unique across the application...

    Can you elaborate or point me to some documentation?

    I never seemed to have this problem in WPF... I'm still learning the differences.

    Tuesday, February 24, 2009 6:54 PM
  • I think having the x:Name for normal control is okay. But there might be some problems with ComboBoxItem only. You can have x:Name for Combobox in your user control and you can use your user control multiple time.
    Tuesday, February 24, 2009 10:19 PM
  • Thanks for the report! This is a bug with XAML namescope in this scenario. We have this on our bug list for the next release.

    Thanks, Stefan Wick

    Wednesday, February 25, 2009 12:02 AM
  • What a terrible waste of perfectly good mspaint art. I didn't realize it was a bug -- it's just never worked for me, so I figured it wasn't allowed.
    Wednesday, February 25, 2009 12:51 AM