简体   繁体   中英

strange behavior on removing items from a bound collection

i have a ListView which is data-bound to an OservableCollection.. i need to make a button that removes items from the collection depending on the selected items of the ListView of the UI.

my XAML :

<ListView Name="MyListView"
          ItemsSource="{Binding}"
          SelectionMode="Multiple"/>

<Button Name="RemoveButton"
        Click="RemoveButton_Click" />

my C# :

 private void Page_Loaded(object sender, RoutedEventArgs e)
    {
        MyListView.DataContext = this.ItemsCollection; 
      // ItemsCollection is the ObservableCollection
    }

  private void RemoveButton_Click(object sender, RoutedEventArgs e)
    {
        foreach (var item in MyListView.SelectedItems)
        {
            ItemsCollection.Remove((Item)item);
        }
    }

now what happens is whenever i click the RemoveButton the first selected item is removed but the one after it is not removed and so on, for example if my ListView shows this :

item 1 item 2 item 3 item 4 item 5 item 6

then selecting all items and hitting Remove makes the ListView shows this :

item 2 item 4 item 6

and with repeating the select and remove shows this :

item 4

so you can see it removes an item and leaves the next .. why this weird behavior ? what am i missing ?

This will work..

private void RemoveButton_Click(object sender, RoutedEventArgs e)
    {
        foreach (var item in MyListView.SelectedItems.Cast<object>().ToList())
        {
            ItemsCollection.Remove((Item)item);
        }
    }

You can see the fullcode here:

public partial class MainWindow : Window
{
    public ObservableCollection<Person> _personList { get; set; }

    public MainWindow()
    {
        InitializeComponent();

        _personList = new ObservableCollection<Person>();
        _personList.Add(new Person() { Name = "Person1" });
        _personList.Add(new Person() { Name = "Person2" });
        _personList.Add(new Person() { Name = "Person3" });
        _personList.Add(new Person() { Name = "Person4" });
        _personList.Add(new Person() { Name = "Person5" });
        _personList.Add(new Person() { Name = "Person6" });

        MyListView.DataContext = this._personList;
    }

    private void RemoveButton_Click(object sender, RoutedEventArgs e)
    {
        foreach (var item in MyListView.SelectedItems.Cast<object>().ToList())
        {
            _personList.Remove((Person)item);
        }
    }
}

public class Person
{
    public string Name { get; set; }
}

<Window x:Class="ListView.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ListView"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <StackPanel>
    <ListView Background="DarkCyan" Foreground="White" Name="MyListView"
      ItemsSource="{Binding}" Height="200" DisplayMemberPath="Name"
      SelectionMode="Multiple"/>

    <Button Name="RemoveButton"
    Click="RemoveButton_Click" Height="100" Width="100" />
    </StackPanel>
</Grid>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM