保持数据绑定WPF ObservableCollection的首选方法?

时间:2021-09-07 20:02:56

I have a class that inherits from ObservableCollection(Of MyObject) and the MyObject class handles INotifyPropertyChanged as it should. When the user updates this data through bound controls the collection will change as expected.

我有一个继承自ObservableCollection(Of MyObject)的类,MyObject类应该处理INotifyPropertyChanged。当用户通过绑定控件更新此数据时,集合将按预期更改。

I have a 'Save' button on the form which is meant to persist this collection to disk as XML by calling MyObject.Save on each item. This logic has been written by me.

我在表单上有一个“保存”按钮,用于通过在每个项目上调用MyObject.Save将此集合作为XML保存到磁盘。这个逻辑是我写的。

My question is, is this the preferred way of persisting objects or is there an attribute or interface I am missing that would tell the WPF databinding / .Net framework which method to use to save / load the data?

我的问题是,这是持久化对象的首选方式,还是我缺少的属性或接口会告诉WPF数据绑定/ .Net框架使用哪种方法来保存/加载数据?

I ask because the WPF samples I have seen omit the saving and loading of real data so I am not sure.

我问,因为我看到的WPF样本省略了实际数据的保存和加载,所以我不确定。

1 个解决方案

#1


I would not expect WPF to provide any data loading/saving capability beyond that provided by the .Net core, such as the standard serialization mechanisms.

我不希望WPF提供超出.Net核心提供的任何数据加载/保存功能,例如标准序列化机制。

You current approach seems fine if it fits your needs.

如果它符合您的需求,您目前的方法似乎很好。

#1


I would not expect WPF to provide any data loading/saving capability beyond that provided by the .Net core, such as the standard serialization mechanisms.

我不希望WPF提供超出.Net核心提供的任何数据加载/保存功能,例如标准序列化机制。

You current approach seems fine if it fits your needs.

如果它符合您的需求,您目前的方法似乎很好。