6

I am getting "Value does not fall within the expected range exception" when adding children to stack panel. This happens even when myStackPanel.Children.Count = 0 just before adding to stackpanel. Any idea why?

void func()
{
          myStackPanel.Children.Clear();        
          List<Docs> lDocs =  docDictionary[ID];
          foreach (Docs lDoc in lDocs)
          {
                 ...
                 Border myTextborder = new Border();                   
                 myTextborder.BorderThickness = new Thickness(1);
                 myTextborder.Name = lDoc.Name;
                 ...

                 myStackPanel.Children.Add(myTextborder);   //Getting Value does not fall within the expected range exception here
          }
}

func() is called multiple times. I read that the error occurs when we attempt to add children with the same name. But in my case, I am clearing the stack panel and the error occurs even if the foreach loop runs just once per call to the func()

Tsu
  • 61
  • 1
  • 1
  • 4
  • What if you comment out the `myTextborder.Name` line? – Gabe May 20 '11 at 03:25
  • Also, the code you replaced with "..." in your example is probably relevant. – Josh May 20 '11 at 03:28
  • The error doesn't happen if I comment it. But for each func call I am clearing stackpanel. Why does this happen. There may be duplicate of myTextBorder.Name from a previous func() call, not the current one. How does that matter when I clear each time. – Tsu May 20 '11 at 03:29
  • @Josh The only change to myStackPanel or myText border in the ... part is myTextborder.Child = myControl; – Tsu May 20 '11 at 03:34
  • If the error is not happening when commenting out the line that sets the name then you are probably setting an invalid name. As stated in my answer below, you have to make sure you're setting a valid identifier name. So for example "myBorder" will work but "My Border" won't. – Josh May 20 '11 at 04:13
  • Does the myTextBorder really need to be named at all? If so why? – AnthonyWJones May 20 '11 at 07:54

4 Answers4

9

This error can be caused when there are two elements being added with the same name. In your case, are there any duplicate lDoc.Name values? If so, you could add an extra unique identifier. For example:

int id = 0; //outside foreach loop

myTextborder.Name = lDoc.Name + id.ToString();
id++;
keyboardP
  • 68,824
  • 13
  • 156
  • 205
0

I have found that this error often occurs when you set the Name property of a control to the same name of an existing control in the Children. My guess is that there are duplicate Names in the collection of Docs. It doesn't always error, but it does sometimes without explanation.

Christian Findlay
  • 6,770
  • 5
  • 51
  • 103
0

Double check the stack trace. Sometimes the line number is off but it's possible that the exception is occurring in the setter for the Name property. It must follow the normal rules for an identifier.

Josh
  • 68,005
  • 14
  • 144
  • 156
0

Seems to me that what you really want is an ItemsControl, you are not really using the capabilities of Silverlight:-

<ScrollViewer>
     <ItemsControl x:Name="items">
         <ItemsControl.ItemTemplate>
             <DataTemplate>
                <Border BorderThickness="1">
                   <TextBlock Text="{Binding Name}" />
                   <!-- what ever xaml you require to represent a document -->
                </Border>
             </DataTemplate>
         </ItemsControl.ItemTemplate>
     </ItemsControl>
</ScrollViewer>

then your func becomes:-

public void func()
{
    items.ItemsSource =  docDictionary[ID];
}
AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306