Silverlight:值未落在预期范围内异常

发布于 2024-11-09 02:36:43 字数 774 浏览 0 评论 0原文

添加时我收到“值未落在预期范围内的异常” 孩子们堆叠面板。即使在添加到 stackpanel 之前 myStackPanel.Children.Count = 0 时也会发生这种情况。知道为什么吗?

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() 被调用多次。我读到当我们尝试添加同名的孩子时会发生错误。但就我而言,我正在清除堆栈面板,即使 foreach 循环每次调用 func() 只运行一次,也会发生错误

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凡尘雨 2024-11-16 02:36:43

当添加两个同名元素时,可能会导致此错误。在您的情况下,是否有重复的 lDoc.Name 值?如果是这样,您可以添加额外的唯一标识符。例如:

int id = 0; //outside foreach loop

myTextborder.Name = lDoc.Name + id.ToString();
id++;

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++;
作业与我同在 2024-11-16 02:36:43

仔细检查堆栈跟踪。有时行号关闭,但异常可能发生在 Name 属性的设置器中。它必须遵循标识符的正常规则。

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.

和影子一齐双人舞 2024-11-16 02:36:43

在我看来,您真正想要的是一个 ItemsControl,您并没有真正使用 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>

那么您的 func 就变成:-

public void func()
{
    items.ItemsSource =  docDictionary[ID];
}

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];
}
清秋悲枫 2024-11-16 02:36:43

我发现当您将控件的 Name 属性设置为与 Children 中现有控件的名称相同时,经常会发生此错误。我的猜测是文档集合中有重复的名称。它并不总是错误,但有时会在没有解释的情况下发生错误。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文