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 be
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];
}
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.
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.
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++;