C#WPF ListView在删除时不会从ObservableCollection更新项目

时间:2017-07-31 18:44:08

标签: c# wpf listview observablecollection

尝试将selectableListViewItemsSource binded移至ObservableCollectionDebugging表示项目已正确标记为已选中并已删除列表本身。但显示的ListView并未更新更改,因此已删除的项目会保留shown

查看

<ListView BorderThickness       = "0"
                          AlternationCount      = "2"
                          BorderBrush           = "Transparent"
                          ItemsSource           = "{Binding TestEntries}"
                          Style                 = "{StaticResource ListViewStyle}"
                          ItemContainerStyle    = "{StaticResource ListViewItemStyle}"
                          ScrollViewer.HorizontalScrollBarVisibility="Hidden">

可观察的收藏

public ObservableCollection<Entry> TestEntries
{
    get => new ObservableCollection<Entry>(_testEntries
           .Where(x => x.Title.ToUpper().Contains(FilterText.ToUpper()))
           .OrderByDescending(x => x.Date));
    set
    {
        _testEntries = value;
        OnPropertyChanged();
    }
}

命令(删除项目)

private void ButtonDeleteCommandExecute()
{
    var toDeleteEntries = TestEntries.Where(x => x.IsSelected);
    toDeleteEntries.ToList().ForEach((obj) =>
    {
        TestEntries.Remove(obj as Entry);
    });
}

希望有人能指出我正确的方向

1 个答案:

答案 0 :(得分:0)

试试这个:

ObservableCollection中返回getter的同一对象,以便notifyTarget更新为@George Alexandria Suggested。

public ObservableCollection<Communication> TestEntries
{
   get
   {
      return _testEntries;                  
   }
   set
   {
      _testEntries = value;
      //OnPropertyChanged(); <-- of no use
    }
 }

由于您希望filter中有listView个数据:

TestEntries = new ObservableCollection<Communication>(_testEntries.Where(x=>x.Title.ToUpper().Contains(FilterText.ToUpper())).OrderByDescending(x=>x.Date).ToList());