我是否需要使用 IObservable 来跟踪我的集合的更改?
阅读了大量有关 IObservable 的内容,我想知道该模式在我的场景中会给我带来什么(如果有的话)。
我有一个带有数据网格的 WPF 应用程序,该网格绑定到 Product 的 IObservable 集合。
ProductList.DataContext = AppState.Current.Products
...其中 AppState.Current.Products 返回 ObservableCollection
我的目标是:
- 跟踪已更改对象的列表
- 通知 UI 对象已更改,以便我可以启用 保存按钮
我的产品类看起来像这样...
public class Product
{
string _desc;
public string Description
{
get { return _desc; }
set { _desc = value; }
}
}
现在,如果我更新网格上一行上的描述列,Description
的设置器会被调用。因此,我可以在此处预订一些代码,在我的产品上设置一个 Dirty
标志,然后我可以使用该标志来查询需要持久保存到数据库的已更改产品的列表。
我认为我可以使用 IObservable 来实现我的第二个目标,即通知用户界面一行已更改?或者是否有更简单的方法,例如网格上的某些属性?
如果我要使用 IObservable,我已经看到过该产品公开可以订阅的事件的示例。但是,我不想将事件处理程序连接到我的所有对象。我需要一些东西来告诉我集合中的任何对象何时发生更改。
此外,我如何将此通知连接到我的用户界面。
Read quite a bit about IObservable and I am wondering what the pattern will give me (if anything) in my scenario.
I have a WPF application with a data grid, the grid is bound to an IObservable collection of Product.
ProductList.DataContext = AppState.Current.Products
... where AppState.Current.Products returns ObservableCollection<Product>
My objectives are:
- Track a list of changed objects
- Inform the UI that an object has changed so that I can enable the
save button
My product class looks something like this...
public class Product
{
string _desc;
public string Description
{
get { return _desc; }
set { _desc = value; }
}
}
Now if I update a description column on a row on my grid the setter for Description
does get called. So I could book in some code here to set a Dirty
flag on my product which I could then use to query for a list of changed products that need to be persisted to the DB.
I think that I could use IObservable to meet my second objective of informing the user interface that a row has been changed? Or is there an easier method such as some property on the grid?
If I am to use IObservable I have seen examples where the product exposes events that can be subscribed to. However, I dont want to hook up event handlers to all of my objects. I need something that tells me in general when any object in the collection has changed.
Additionally, how would I hook up this notification to my UI.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ObservableCollection
尽管其名称如此,但与IOberservable
无关。它实现了INotifyCollectionChanged
和INotifyPropertyChanged
,这些是 WPF 在绑定场景中寻找的接口。因此,如果您在定义界面时在 xaml 中使用数据绑定,并绑定到ObservableCommection
,则当集合更改时,绑定将自动收到通知,并且 UI 将更新。您不需要在那里使用 IObservable 。不要误会我的意思,IObservable 是一个非常有用且非常强大的模式,尽管有点难以理解。就我个人而言,我经常使用它。但这不是您应该使用它的情况。
ObservableCollection<Something>
, despite its name, has nothing to do withIOberservable<Something>
. It implementsINotifyCollectionChanged
andINotifyPropertyChanged
, and those are the interfaces WPF looks for in a binding scenario. So if you use data binding in your xaml while defining your interface, and bind to anObservableCommection<Something>
, the binding will be automatically informed when the collection changes, and the UI will be updated. You don't need to use IObservable there.Don't get me wrong, IObservable is a very useful and very powerful pattern, if a little hard to understand. Personnally, I use it a lot. But this is not a case where you should use it.
接口 INotifyPropertyChanged 是在 .Net 框架中实现的类似的任务,根据 MSDN 库:
The interface INotifyPropertyChanged was implemented in the .Net framework for similar tasks, as per MSDN Library:
如果我理解正确的话你想要两件事。
您的 setter 已经通过绑定被调用,因此您可以轻松跟踪它。
我不确定 UI 中的保存按钮存在于何处,但无论它在哪里,您都可以简单地将其启用属性与布尔属性绑定。一旦检测到对象发生更改,请通过将 bool 属性值设置为 true 来启用该按钮。
在这种情况下不需要使用 IObservable。您已经绑定到 ObservableCollection 这应该足够了。您只需要一个简单的绑定来告诉 UI 有关启用或禁用“保存”按钮的信息
If I understand correctly you want to two things.
Your setter is already getting called through the bindings so you can easily track that.
I am not sure where that save button exists in the UI but where ever it is you can simply bind it's enabled property with a boolean property. As soon as you detect a change in your object, enable the button by setting the bool property value to true.
There is no need to use IObservable in this scenario. You are already binding to ObservableCollection that should be enough. You just need a simple binding to tell UI about enabling or disabling the Save button