编辑ObservableCollection的SelectedItem无绑定控件查看编辑,直到它们保存 [英] Editing ObservableCollection's SelectedItem Without Bound Controls Seeing Edits Until They Are Saved
问题描述
我有一个包含 ListView
和编辑按钮的视图。 ListView的 ItemSource
被绑定到底层视图模型上的 ObservableCollection< Account>
属性。它的 SelectedItem
属性也绑定到视图模型。
点击编辑按钮时,现有视图模型启动编辑视图/视图模型对(编辑屏幕),允许用户编辑当前选择的帐户
。要编辑的帐户
由主视图模型的 SelectedItem
属性确定。
问题:在编辑屏幕上进行的任何更改都会立即反映在其他屏幕的 ListView
中,即使在编辑屏幕的保存按钮。为什么会发生这种情况有意义 - Account
是在属性更改时提高属性更改事件, ListView
正在处理
可能的解决方案
- 暂停帐户在编辑时的媒体资源更改通知正在进行中。缺点:如果在编辑
Account
实例时执行手动数据绑定更新,则正在进行更改将显示在ListView
,即使这些更改没有提高通知。此外,如果用户为同一帐户
启动第二个编辑窗口,他们将看到正在进行更改。 c> EditingAccount 在code>类,只有当
Save()
被调用时,才将它所做的更改复制回原来的Account
。编辑屏幕是否应该负责促进这种包装,还是应该要求服务层执行此操作?
的这些选项?你遇到它时如何解决这个问题?
我会用一些版本的第二个选项。基本上,这是 MVVM 模式的变体这被认为是正确的做WPF / Silverlight代码的方式。基本上你应该有一个ModelView对象,每个屏幕(视图)包装模型,并以特定于视图的格式公开的模型,所以它确切的View需要什么和更多。
I have a view containing a ListView
and an "Edit" Button. The ListView's ItemSource
is bound to an ObservableCollection<Account>
property on the underlying view model. Its SelectedItem
property is also bound to the view model.
When the edit button is clicked, the existing view model launches an editing view/view model pair ("editing screen") allowing the user to edit the currently selected Account
. The Account
to edit is determined by the main view model's SelectedItem
property.
Problem: Any changes made in the editing screen are immediately reflected in the other screen's ListView
, even before the edit screen's "Save" button is clicked. Why this happens makes sense--Account
is raising property change events when its properties are changed and the ListView
is processing those notifications.
Desired Outcome: Bound controls (like the ListView
) should only see editing screen changes after"Save" is clicked.
Possible Solutions
- Suspend Account's property change notifications while editing is underway. Disadvantages: If a manual data-binding update is performed while an
Account
instance is being edited, the "in-progress" changes will appear on theListView
even though those changes haven't been raising notifications. Also, if the user launches a second edit window for the sameAccount
, they will see the "in-progress" changes. Idea rejected. - Have the editing screen view model wrap the
Account
instance in some kind ofEditingAccount
class that copies changes made to it back to the originalAccount
only whenSave()
is called. Should the editing screen take on the responsibility of facilitating this wrapping or should it ask the service layer to do it?
What do you think of these options? How do you solve this problem when you encounter it?
I would go with some version of the second option. Basically this is a variation of the MVVM pattern which is considered the "right" way to do WPF/Silverlight code. Basically you should have a ModelView object for each "screen" (View) that wraps the model and exposes the model in a format specific to the View so it does exactly what the View needs and NO MORE.
这篇关于编辑ObservableCollection的SelectedItem无绑定控件查看编辑,直到它们保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!