簡體   English   中英

從綁定的集合中刪除項目的奇怪行為

[英]strange behavior on removing items from a bound collection

我有一個ListView,它與OservableCollection數據綁定。.我需要創建一個按鈕,該按鈕根據UI的ListView的選定項從集合中刪除項。

我的XAML:

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

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

我的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);
        }
    }

現在發生的事情是,每當我單擊RemoveButton時,第一個選定的項目將被刪除,但之后的那個項目卻未被刪除,依此類推,例如,如果我的ListView顯示以下內容:

項目1項目2項目3項目4項目5項目6

然后選擇所有項目並單擊Remove使ListView顯示以下內容:

項目2項目4項目6

並重復選擇和刪除顯示以下內容:

項目4

所以您可以看到它刪除了一個項目並留下了下一個..為什么會有這種奇怪的行為? 我想念什么?

這將工作..

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

您可以在此處查看完整代碼:

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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM