Silverlight: Value does not fall within the expected range exception

前端 未结 4 633
臣服心动
臣服心动 2020-12-19 09:35

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

相关标签:
4条回答
  • 2020-12-19 10:08

    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];
    }
    
    0 讨论(0)
  • 2020-12-19 10:19

    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.

    0 讨论(0)
  • 2020-12-19 10:27

    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.

    0 讨论(0)
  • 2020-12-19 10:31

    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++;
    
    0 讨论(0)
提交回复
热议问题