我的屏幕有一个Person类型的对象列表,以及一个通过数据绑定显示人员属性(名称,地址,重量等)的表单。
我希望能够:
单击“编辑属性”按钮,然后开始修改人员的属性(我已经可以使用ViewModel中的Command执行此操作); 如果我单击“取消”,编辑的信息“回滚”到该人的原始未修改值; 如果我单击“保存更改”,那么此人的姓名将在人员列表中更改。我现在的问题是,当我正在编辑表单时,原始属性会更新,所以说,实时,如果我决定取消,我没有原始值“返回”。
我考虑Clone选定的人,但这似乎很奇怪。 我认为更好的方法是仅更改字段的文本属性,并仅在单击提交更改时更新回来,但我不知道如何操作,特别是如何保持数据绑定一致性。
My screen has a list of objects of type Person, and a form that displays the person properties (name, address, weight, etc.) via databinding.
I want to be able to:
Click in a button "Edit properties", and start modifying the person's properties (I can already do that using Commands in the ViewModel); If I click "Cancel", the edited info "rollback" to the original unmodified values of that person; If I click "Save changes", JUST THEN the person's name changes in the person list.My problem right now is that, as I am editing the forms, the original properties are updated, so to say, in realtime, and if I decide to cancel, I have not the original values to "go back".
I considered to Clone the selected person, but that seemed to be odd. I think a better approach would be to change only the text properties of the field, and update back only when clicking to submit changes, but I don't know how to do it, specially how to preserve databinding consistency.
最满意答案
绑定时,使用UpdateSourceTrigger = Explicit 。 这将告诉XAML在你告诉它之前不要更新源绑定。 然后,当单击“保存”按钮时,可以在绑定上调用UpdateSource以将控件的内容推送回Person对象,如:
var nameBinding = nameTextBox.GetBindingExpression( TextBox.TextProperty ); nameBinding.UpdateSource();要取消,请改用UpdateTarget ,它会将Person对象中的数据推送回您的控件。
When you bind, use UpdateSourceTrigger=Explicit. That will tell XAML not to update the source binding until you tell it to. Then, when your Save button is clicked, you can call UpdateSource on the binding to push the contents of your controls back to the Person object, something like:
var nameBinding = nameTextBox.GetBindingExpression( TextBox.TextProperty ); nameBinding.UpdateSource();To cancel, use UpdateTarget instead, which will push the data in the Person object back to your control.
更多推荐
发布评论