Note that there are some explanatory texts on larger screens.

plurals
  1. POSilverlight ListBox throws out of range exception when changing SelectedItem
    text
    copied!<p>I have some Silverlight XAML that used to work, but I can't seem to figure out what I did to suddenly make it stop working, or even how to change it.</p> <p>Here's some XAML:</p> <pre><code>&lt;ListBox ItemsSource="{Binding ItemsForSelectedPublisher}" SelectedItem="{Binding SelectedItemForPublisher, Mode=TwoWay}" DisplayMemberPath="ItemNameWithSelectionCount" SelectionMode="Single" HorizontalAlignment="Left" FontSize="14" Width="300" Height="500" /&gt; </code></pre> <p>And some code from the view model:</p> <pre><code> public ObservableCollection&lt;ItemViewModel&gt; ItemsForSelectedPublisher { get { return _itemsForSelectedPublisher; } private set { if (_itemsForSelectedPublisher != value) { _itemsForSelectedPublisher = value; RaisePropertyChanged(() =&gt; ItemsForSelectedPublisher); } } } public ItemViewModel SelectedItemForPublisher { get { return _selectedItemForPublisher; } set { if (_selectedItemForPublisher != value) { _selectedItemForPublisher = value; RaisePropertyChanged(() =&gt; SelectedItemForPublisher); } } } </code></pre> <p>After the ListBox's SelectedItem is changed (set for the first time), the following exception gets caught in the Silverlight Application.UnhandledException Handler.</p> <pre class="lang-none prettyprint-override"><code>Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index at System.ThrowHelper.ThrowArgumentOutOfRangeException() at System.Collections.Generic.List`1.get_Item(Int32 index) at System.Windows.Automation.Peers.SelectorAutomationPeer.RaiseSelectionEvents(SelectionChangedEventArgs e) at System.Windows.Controls.Primitives.Selector.InvokeSelectionChanged(List`1 unselectedItems, List`1 selectedItems) at System.Windows.Controls.Primitives.Selector.SelectionChanger.End() at System.Windows.Controls.Primitives.Selector.SelectionChanger.SelectJustThisItem(Int32 oldIndex, Int32 newIndex) at System.Windows.Controls.ListBox.MakeSingleSelection(Int32 index) at System.Windows.Controls.ListBox.HandleItemSelection(ListBoxItem item, Boolean isMouseSelection) at System.Windows.Controls.ListBox.OnListBoxItemClicked(ListBoxItem item) at System.Windows.Controls.ListBoxItem.OnMouseLeftButtonDown(MouseButtonEventArgs e) at System.Windows.Controls.Control.OnMouseLeftButtonDown(Control ctrl, EventArgs e) at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName, UInt32 flags) </code></pre> <p>From what I can tell, the ListBox is getting some incorrect index but no idea what it is or why that's even happening. Anyone have an idea how this could be happening? At this point I don't know where else to look.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload