2013-03-17 39 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()覆盖。这导致列表框绑定失控并抛出超出范围的异常。

相关问题