Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit Cancel out of updates from data bindings
4 replies. Latest Post by roarfred on July 22, 2008.
(0)
roarfred
Member
45 points
14 Posts
07-21-2008 2:59 PM |
I've been looking for an answer to this tonight, and suppose I am just looking for a confirmation. (While still hoping for a solution)
In my control, I have a couple of textboxes bound in twoway mode. Updating one or the other causes the new values to be written to my datasource (at the lostfocus event I suppose). I'd like to cancel the changes, and revert to my original values...
1) I have a save and a cancel button. I'd like the save button to fire an event, where I let the user cancel out of the changes if she chooses to. Is there a way to postpone the write from the binding, and just manually activate it from the save button (after the event was called)?
2) The textboxes get their datacontext from a level above in the xaml. (DataContext on a grid etc.) Can interfer with when/how the values are written from the textboxes at this level somehow? (If this is really far out, please just ignore it... or should I ask: will it help sell my soul?)
3) There's a BindingManagerBase in Winforms which can do begin/end/cancel edits. Anything like that in this world?
4) I suspect my options are to implement support for begin/end/cancel editing in my datasource. I can make an interface with these methods and make my control aware of them, and this should work pretty well. As long as I made the datasource... Question is, is there such an interface available, which is meant for such kind of use? The only control I see have similar capabilities is the DataGrid, which seems to implement this part internally.
PS: For option 1, this wouldn't bee too hard if I knew which controls I have. A part of the problem is that an "editor" is provided as content by the user.
If needed, there's more information on how the control was made here: http://roarfred.blogspot.com/2008/07/part-2-creating-silverlight-control.html
Suggestions are highly appreciated!
Regards,Roar
sladapter
All-Star
17427 points
3,171 Posts
07-21-2008 5:05 PM |
I don't think the Data Object has your original data remembered some where. So once user update your data object, you lost your original data unless you keep a copy of your original data. So before you bind your data to control, make another copy of your data. Use that when you try to cancel the changes.
07-22-2008 1:12 AM |
Thanks (again) for your response, sladapter!
I am getting a bit confused as to who I should throw this ball... In my opinion, the cancel operation belongs to the data object, and having it there will also allow for using the existing twoway binding syntax. Drawback is that the control becomes dependent on a proprietary interface. Placing the functionality inside of the control would work too, but I'd probably have to write the binding/update code manually.
Looking at the DataGrid, it supports cancel at row-level while binding to any IList datasource. Do we know how this was done? Is the source code still available for this?
Any gut-feeling for which approach will be the best? (Or, if this was included in the SL framework, where would it have been?)
Regards,Roar Fredriksen
07-22-2008 11:20 AM |
Yes, if you want to make the cancel operation on data object, you need to write code for your data object.
You can implement IEditableObject for your DataObject. So you write BeginEdit/CancelEdit/EndEdit functin:
http://msdn.microsoft.com/en-us/library/system.componentmodel.ieditableobject.aspx
Basically you have one copy of data as backup data and one copy of current data. When you CancelEdit, you copy your backup data to the current data. So it's the same idea as I said in the first post. But you wrap it in the object level.
By the way, I just took a look at DataGrid code. If you want to use the cancelEdit feature in DataGrid you also need to make sure your DataObject is IEditableObject.
If you want to see how DataGrid is doing this, you can use Lutz Roeder's .Net Reflector to peek the code.
07-22-2008 11:35 AM |
As so many times before, THANKS sladapter!
This is exactly what I was looking for. Wonder how I missed it now :)