Skip to content

System.Reflection.TargetInvocationException:“Exception has been thrown by the target of an invocation.” #5

@CodingOctocat

Description

@CodingOctocat

I have a ObservableCollectionEx: [1, 2, 3], if i remove 2 and 3 in DelayNotifications, then throw

System.Reflection.TargetInvocationException:“Exception has been thrown by the target of an invocation.”
InnerEx: NotSupportedException(Range operation is not supported)

<Window x:Class="ObservableCollectionExIssue5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ObservableCollectionExIssue5"
        Title="MainWindow"
        Width="300"
        Height="450"
        d:DataContext="{d:DesignInstance Type=local:MainWindow,
                                         IsDesignTimeCreatable=True}"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        FontSize="24"
        mc:Ignorable="d">
    <Grid>
        <ListBox x:Name="lst"
                 ItemsSource="{Binding Samples}"
                 SelectionMode="Extended" />

        <Button Height="50"
                VerticalAlignment="Bottom"
                Click="Button_Click"
                Content="Delete selected items" />
    </Grid>
</Window>
using System.Collections.Immutable;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;

namespace ObservableCollectionExIssue5;

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public ObservableCollectionEx<int> Samples { get; set; } = new() { 1, 2, 3, 4, 5 };

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var toBeDeleted = lst.SelectedItems.Cast<int>().ToImmutableList();

        using var d = Samples.DelayNotifications();

        for (int i = 0; i < toBeDeleted.Count; i++)
        {
            int item = toBeDeleted[i];
            d.Remove(item);
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions