问题标签 [inotifycollectionchanged]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
1410 浏览

wpf - 取消删除操作 - NotifyCollectionChangedAction

我在我的视图模型中使用以下代码从集合中删除项目:

我有一个绑定到集合的数据网格。我想知道是否有根据确认提示取消删除操作?我注意到 NotifyCollectionChangedEventArgs 没有取消方法。发生的情况是,当用户从数据网格中删除一个项目但在确认时选择“否”时,该项目仍会从数据网格中删除。它不会从数据库中删除,如果刷新数据网格,它将再次出现。我正在使用 mvvm 模式,我更喜欢这样做,而不必编写我的数据网格。任何帮助表示赞赏。

0 投票
1 回答
506 浏览

c# - WPF如何分离用户控件中的事件挂钩

我有一个UserControl包含ComboBox. 我需要将事件侦听器附加到ComboBox.Items集合中。

但我似乎无法弄清楚如何分离它。还是在整个控件被垃圾收集时自动处理?

0 投票
2 回答
282 浏览

c# - NotifyCollectionChangedAction:移除对象实例?

我目前正在为一个集合实现 INotifyCollectionChanged 接口,该集合通常具有非常关键和短暂的项目。所有这些项目都实现了 IDispose,可以在从集合中删除之前立即调用它。我对销毁命令没有任何控制权,我只能顺其自然。

我现在的麻烦是,如何传播“删除”操作。NotifyCollectionChangedAction 构造函数确实提供了一个仅指定操作的变体,但我确实有已删除项目的索引。可悲的是,没有仅采用索引的重载。

所以我想我可以:

  • 仅通过“删除”操作
  • 将删除操作与“空”对象和索引一起传递
  • 将删除操作与已处置的对象和索引一起传递

你更喜欢其中的哪一个?我有点害怕错误地实现这样一个“核心”接口,并可能导致不明显相关的错误......

我找不到任何指南,“更改”列表中的项目会发生什么,有可用的吗?

旁注:如果这很重要,该集合不会立即绑定到 WPF。

0 投票
3 回答
6591 浏览

.net - 如何使用 ConcurrentDictionary、INotifyCollectionChanged、INotifyPropertyChanged 创建自定义可观察集合

我正在尝试创建一个 ObservableConcurrentDictionary。该对象将在多线程应用程序中使用,它的数据用于通过控件的 ItemsSource 属性填充控件。

这是我提出的实现:

不幸的是,该解决方案没有按预期工作 - 事实上,它根本不起作用。关于我做错了什么或是否存在任何更好的解决方案的任何想法?

请注意我不能使用 ObservableCollection,因此我必须编写自己的 Observable 集合。

编辑:工作版本如下。希望这可以帮助其他有类似问题的人。

0 投票
1 回答
6713 浏览

c# - How to get property name from the sender object of an INotifyPropertyChanged PropertyChanged event

I have a base DependencyObject class where I have a method that takes an object, gets the properties, and for each property that is a type that implements INotifyPropertyChanged, I add a new PropertyChangedEventHandler. Now in the handler method, it gets the parameters of an object "sender" and the PropertyChangedEventArgs "e". My question is, does anyone know how to dynamically get the property name if sender is a property of a type that implements the INotifyPropertyChanged.

Here is what I'm working with:

The SetValues method's first param is the DependencyObject type that will be used in the view model. The second param is the entity that is being returned from the DomainService's Context.LoadOperation.

What my issue boils down to is when the INotifyCollectionChanged.CollectionChanged fires I'm needing to be able to raise the PropertyChanged event with the collection's property name. So if anyone has any advise I would greatly appreciate it. Thanks in advance.

Edit
Figured out how to get the properties name that is firing the event. Here is an edited version of my PropertyChangedEventHandler.

Basically this does what I was looking for, but aparentyly I am still not doing something right. The UIElement is still not updating when the ObservableCollection that is a property of another type is being added to.

Here is an example of my DependencyObjects and ViewModel:

0 投票
3 回答
18791 浏览

wpf - 什么是 notifycollectionchangedaction 重置值

我有一个可观察的集合...... SelectableDataContext<T>..在泛型类SelectableDataContext<T>中......有两个私有成员变量

  1. 私人 T 项目。
  2. 私有 bool isSelected。

当 IsSelected 属性更改时...我的集合的更改属性未触发。

我认为它应该开火……因为它ResetINotifyCollectionChangedAction.

0 投票
2 回答
3926 浏览

c# - Observable Collection 上的集合更改事件示例

我在 WPF 应用程序中有一个列表框,它显示照片对象的可观察集合。将照片添加到集合中时,UI 需要立即显示新图像。我知道这可以使用 CollectionChanged 事件来处理。我四处寻找有关如何使用句柄集合更改事件的示例,但我没有找到任何有效的方法。有谁知道任何好的例子?

另一件事是图像来自我计算机上的一个目录,我有一个文件系统观察者监视该目录是否有新照片被添加或删除。我目前正在使用文件系统事件处理程序在添加或删除照片时更新集合,但问题是当我将新照片添加到目录时,抛出异常说我无法从不是线程的线程修改集合主线程。有谁也知道如何解决这个问题?这是此问题的代码:

0 投票
1 回答
11469 浏览

wpf - 实现 INotifyCollectionChanged 接口

我需要实现一个具有特殊功能的集合。另外,我想将这个集合绑定到一个ListView,所以我最终得到了下一个代码(我在论坛中省略了一些使它更短的方法):

我想用一个简单的数据类来测试它:

所以我创建了一个 myCollection 类的实例,如下所示:

问题是尽管我实现了 INotifyCollectionChanged 接口,但集合更新时 ListView 不会更新。

我知道我的绑定很好(在 XAML 中),因为当我使用 ObservableCollecion 类而不是这样的 myCollecion 类时:

ListView 更新

问题是什么?

0 投票
3 回答
3238 浏览

wpf - 我可以在集合更改事件上回滚集合更改吗?

我有 2 个列表视图...并在它们之间添加/删除按钮。

在视图模型中列表视图集合的集合更改事件中,我可以回滚特定条件的更改吗?

0 投票
1 回答
440 浏览

wpf - 将数据插入 XML 文件通知 ObservableCollection

我有一个父窗口,它有一个绑定到从 XML 文件中获取数据的 ObservableCollection 的 ListView。在父窗口上,我有一个添加按钮,可以打开一个模态窗口(form2.ShowDialog(),我认为这是一个模态窗口)。

在子窗口中,我有几个文本字段和一个添加按钮,用户输入新记录的数据并单击添加按钮,XML 文件将更新为新记录。

我想要做的是当子窗口关闭时,父窗口中的 ListView 被刷新以显示新记录。

当我从子窗口更新 XML 文件时,我直接对 XML 文件执行此操作,我是否应该以其他会在 ObservableCollection 上引发事件的方式将数据添加到文件中?

我正沿着向父窗口上的添加按钮添加事件处理程序的路径前进,并尝试强制重新读取 XML 文件,但我不知道如何实现这一点,以下是我到目前为止所做的.

form1.xaml.vb:

我无法锻炼应该进入 refresh_EmContacts 子的内容,我是否需要调用某种 ObservableCollection 事件处理程序来通知它已更新并重新读取?

任何帮助将不胜感激。

马特