2013-03-17 42 views
0

我有一些Silverlight XAML曾經工作過,但我似乎無法弄清楚我做了什麼以後突然讓它停止工作,甚至無法改變它。Silverlight ListBox在更改SelectedItem時拋出了範圍異常

下面是一些XAML:

<ListBox ItemsSource="{Binding ItemsForSelectedPublisher}" 
     SelectedItem="{Binding SelectedItemForPublisher, Mode=TwoWay}" 
     DisplayMemberPath="ItemNameWithSelectionCount" 
     SelectionMode="Single" 
     HorizontalAlignment="Left" 
     FontSize="14" 
     Width="300" 
     Height="500" 
     /> 

而且從視圖模型的一些代碼:

public ObservableCollection<ItemViewModel> ItemsForSelectedPublisher 
    { 
     get { return _itemsForSelectedPublisher; } 
     private set 
     { 
      if (_itemsForSelectedPublisher != value) 
      { 
       _itemsForSelectedPublisher = value; 
       RaisePropertyChanged(() => ItemsForSelectedPublisher); 
      } 
     } 
    } 

    public ItemViewModel SelectedItemForPublisher 
    { 
     get { return _selectedItemForPublisher; } 
     set 
     { 
      if (_selectedItemForPublisher != value) 
      { 
       _selectedItemForPublisher = value; 
       RaisePropertyChanged(() => SelectedItemForPublisher); 
      } 
     } 
    } 

ListBox的的SelectedItem改變後(首次設置),以下異常被夾在Silverlight Application.UnhandledException Handler。

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) 

從我可以告訴,列表框越來越一些不正確的索引,但不知道它是什麼,或者爲什麼甚至發生。任何人都有一個想法如何發生這種情況?在這一點上,我不知道還有什麼地方需要注意。

+0

您能否爲此提供更多的信息或代碼。 – Shrikey 2013-03-17 16:57:59

+0

你可以發佈你的ItemViewModel的代碼嗎? – 2013-03-19 18:20:11

回答

0

經過大量的研究,我發現根本原因是我自己的錯(沒有真正的驚喜)。顯然,我自己的代碼中的錯誤是對我的一些視圖模型的一個不正確的Equals()覆蓋。這導致列表框綁定失控並拋出超出範圍的異常。

相關問題