问题描述
我有一个包含的ListView
和编辑按钮的视图。 ListView的的ItemSource
绑定到一个的ObservableCollection<&帐户GT;
底层视图模型属性。它的的SelectedItem
属性也绑定到视图模型。
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.
在单击编辑按钮,在现有的视图模式启动当前选择的编辑视图/视图模型对(编辑画面),允许用户编辑帐户
。在帐户
编辑在主视图模型的的SelectedItem
属性决定的。
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.
问题:在编辑界面所做的任何更改会立即反映在其他屏幕的的ListView
,甚至在编辑屏幕的保存按钮被点击。为什么出现这种情况是有道理的 - 帐户
是提高属性更改事件,当它的属性改变,的ListView
正在处理那些。通知
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.
期望的结果:绑定控件(如的ListView
)应该只看到经过编辑画面变化保存点击。
Desired Outcome: Bound controls (like the ListView
) should only see editing screen changes after"Save" is clicked.
- 暂停帐户的属性更改通知,而编辑正在进行中。缺点:如果手动数据绑定更新而一个
帐户
实例正在编辑完成的正在进行的变化将出现在的ListView
尽管这些变化没有得到提高的通知。另外,如果用户启动第二个编辑窗口为同一帐户
,他们将看到正在进行的转变。想法被拒绝。 - 有编辑屏幕视图模型包裹
帐户
例如在某种EditingAccount
类,拷贝改变它使回原来的帐户
只有当保存()
被称为。如果编辑画面承担促进这一包装的责任,或者应该问的服务层做呢?
- 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?
推荐答案
我会去某个版本的第二个选项。基本上,这是格局的变化而被认为是正确的方式做WPF / Silverlight的代码。基本上你应该有一个模型视图对象为每一个屏幕(查看),它包装的型号和公开的格式特定于视图,以便它不正是模型什么的查看需求并没有更多的。
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不绑定控件眼看编辑,直到它们被保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!