silverlight listbox throwing “value not fall in expected range” even when empty?

梦想的初衷 提交于 2019-12-06 01:32:09

Thanks to all the suggestions. I was able to track it down. At another point in time of the life cycle of the silverlight control, there was another listbox completely separate from this one that was adding an item of the same name. being that both the parent listboxes resided on the same silverlight control, it was blowing up.

I apologize in advance for this being a "non-answer" answer, but maybe it will help.

I had the exact same issue, but with an Accordion instead of a ListBox. My control hierarchy was:

  • UserControl (the page)
    • Accordion
      • AccordionItem0
        • ContentControl
          • ReportView0 (dynamically created at runtime and bound to the ContentControl's Content property)
      • AccordionItem1
        • ContentControl
          • ReportView1 (dynamically created at runtime and bound to the ContentControl's Content property)
      • etc.

I was able to populate the accordion's ItemSource and create the ReportView controls the first time, but if I tried to refresh the list (say, to sort it), I would get the "Value does not fall within the expected range" exception.

I believe what was happening was that, when I freshed the list, my manually created ReportView items were never being disassociated from their original parent ContentControls. After the refresh, when attempting to bind the ReportViews to a different AccordionItem, the exception was being thrown because each ReportView was already in the visual tree.

My solution was to get rid of the ContentControl and declare the ReportView control in its place. This way, Silverlight would manage the control in the visual tree.

(I originally used the dynamic approach so that I could use MEF to satisfy imports via GetExportedValue. When I found out about CompositionInitializer.SatisfyImports, I was able to switch to declaring the ReportView in XAML.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!