我有一些过去可以工作的 Silverlight XAML,但我似乎无法弄清楚我做了什么突然让它停止工作,甚至不知道如何更改它。
这是一些 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 处理程序中捕获了以下异常。
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)
据我所知,ListBox 得到了一些不正确的索引,但不知道它是什么或为什么会发生这种情况。有人知道这是怎么发生的吗?在这一点上,我不知道还能去哪里看。