Expected range error when removing last item from listbox's observablecollection

自古美人都是妖i 提交于 2019-12-12 19:12:08

问题


I have a SL4 ListBox full of items. Here is the ItemContainerStyle:

  <Style x:Key="DataItemStyle"
     TargetType="ListBoxItem">
<Setter Property="Background"
        Value="Transparent" />
<Setter Property="TabNavigation"
        Value="Local" />
<Setter Property="HorizontalContentAlignment"
        Value="Center" />
<Setter Property="Template">
  <Setter.Value>
    <ControlTemplate TargetType="ListBoxItem">
      <Grid Background="{TemplateBinding Background}"
            LLamasoft:DragDropBehaviors.DragDropManager="{Binding ScratchPad.TableExplorer.DragDropManager, Source={StaticResource ViewModels}}"
            LLamasoft:DragDropBehaviors.DragSourceEffect="Copy">
        <ToolTipService.ToolTip>
          <c1:C1DataGrid ItemsSource="{Binding Preview}"
                         CanUserAddRows="False"
                         CanUserEditRows="False"
                         LLamasoft:DataGridBehaviors.FilterIdentityColumn="true" />
        </ToolTipService.ToolTip>
        <VisualStateManager.VisualStateGroups>
          <VisualStateGroup x:Name="CommonStates">
            <VisualState x:Name="Normal" />
            <VisualState x:Name="MouseOver">
              <Storyboard>
                <DoubleAnimation Duration="0"
                                 To=".35"
                                 Storyboard.TargetProperty="Opacity"
                                 Storyboard.TargetName="fillColor" />
              </Storyboard>
            </VisualState>
            <VisualState x:Name="Disabled">
              <Storyboard>
                <DoubleAnimation Duration="0"
                                 To=".55"
                                 Storyboard.TargetProperty="Opacity"
                                 Storyboard.TargetName="contentPresenter" />
              </Storyboard>
            </VisualState>
          </VisualStateGroup>
          <VisualStateGroup x:Name="SelectionStates">
            <VisualState x:Name="Unselected" />
            <VisualState x:Name="Selected">
              <Storyboard>
                <DoubleAnimation Duration="0"
                                 To=".75"
                                 Storyboard.TargetProperty="Opacity"
                                 Storyboard.TargetName="fillColor2" />
              </Storyboard>
            </VisualState>
          </VisualStateGroup>
          <VisualStateGroup x:Name="FocusStates">
            <VisualState x:Name="Focused">
              <Storyboard>
                <ObjectAnimationUsingKeyFrames Duration="0"
                                               Storyboard.TargetProperty="Visibility"
                                               Storyboard.TargetName="FocusVisualElement">
                  <DiscreteObjectKeyFrame KeyTime="0">
                    <DiscreteObjectKeyFrame.Value>
                      <Visibility>Visible</Visibility>
                    </DiscreteObjectKeyFrame.Value>
                  </DiscreteObjectKeyFrame>
                </ObjectAnimationUsingKeyFrames>
              </Storyboard>
            </VisualState>
            <VisualState x:Name="Unfocused" />
          </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <Rectangle x:Name="fillColor"
                   Fill="#FFBADDE9"
                   IsHitTestVisible="False"
                   Opacity="0"
                   RadiusY="1"
                   RadiusX="1" />
        <Rectangle x:Name="fillColor2"
                   Fill="#FFBADDE9"
                   IsHitTestVisible="False"
                   Opacity="0"
                   RadiusY="1"
                   RadiusX="1" />
        <Grid Margin="{TemplateBinding Padding}"
              HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}">
          <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition />
          </Grid.ColumnDefinitions>
          <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
          </Grid.RowDefinitions>
          <SaaS_icons:document_table Margin="4"
                                     Height="40"
                                     Grid.Column="1" />
          <ContentPresenter x:Name="contentPresenter"
                            ContentTemplate="{TemplateBinding ContentTemplate}"
                            Content="{Binding Name}"
                            Grid.Row="1"
                            Grid.ColumnSpan="3"
                            HorizontalAlignment="Center" />
        </Grid>
        <Rectangle x:Name="FocusVisualElement"
                   RadiusY="1"
                   RadiusX="1"
                   Stroke="#FF6DBDD1"
                   StrokeThickness="1"
                   Visibility="Collapsed" />
        <c1:C1ContextMenuService.ContextMenu>
          <c1:C1ContextMenu>
            <c1:C1MenuItem Header="Edit Info"
                           Command="{Binding ScratchPad.TableExplorer.EditInfoCommand, Source={StaticResource ViewModels}}"
                           CommandParameter="{TemplateBinding DataContext}" />
            <c1:C1MenuItem Header="Geocode"
                           Command="{Binding ScratchPad.TableExplorer.GeocodeTableCommand, Source={StaticResource ViewModels}}"
                           CommandParameter="{TemplateBinding DataContext}" />
            <c1:C1MenuItem Header="Delete"
                           Command="{Binding ScratchPad.TableExplorer.DeleteCommand, Source={StaticResource ViewModels}}"
                           CommandParameter="{TemplateBinding DataContext}" />
          </c1:C1ContextMenu>
        </c1:C1ContextMenuService.ContextMenu>
      </Grid>
    </ControlTemplate>
  </Setter.Value>
</Setter>

The ItemsSource is set to an ObservableCollection in a VM. As seen, there is a context menu option to delete an item from the list. I can add and remove items all day long with no issues, until the last item is removed. Then I get the error:

Value does not fall within the expected range

I see most people calling it an issue with the x:Name elements in the templates, but I didn't see any issue, and was greatly puzzled when I only had the issue removing the last one. Can anyone help me out? This is all there is in the call stack:

System.Windows.dll!MS.Internal.Error.CallApplicationUEHandler(System.Exception e) + 0x4d bytes  
System.Windows.dll!MS.Internal.Error.CallAUEHandler(uint hr, out uint bIsHandled = 0) + 0x14 bytes  
[Appdomain Transition]  

来源:https://stackoverflow.com/questions/8680067/expected-range-error-when-removing-last-item-from-listboxs-observablecollection

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