0

How to declare a BindingSource in VB using WPF? I can't use a command like BindingSource.CancelEdit() like I used to when using Windows Form instead of WPF...

curzedpirate
  • 147
  • 1
  • 3
  • 15
  • http://www.codeproject.com/Articles/29054/WPF-Data-Binding-Part-1 vb.net and c# look a like - so mabye this can help you untill someone answers your question. – deltu100 Oct 12 '12 at 09:00

2 Answers2

2

You could use a BindingGroup for that: set a BindingGroup on an element in XAML which contains all the controls that edit your object. By default, this will set the UpdateSourceTrigger on these controls to Explicit, which means that you will have to call BindingGroup.UpdateSources in order to actually change the properties of the object which is currently edited. So, you would do that in a Submit command or something similar.

If you want to cancel the edit, you can do that using BindingGroup.CancelEdit. This will throw away the cached values in the controls and reset them to the values of the bound properties.

I think this is a lot easier than implementing IEditableObject or a Memento...

hbarck
  • 2,934
  • 13
  • 16
  • @hbarck - I did it ^_^ thanks! I have one last question I hope you don't mind... In my XAML, I created the BindingGroup on an element as you said (I put it in a Grid element). Then, on my VB I refer to that BindingGroup like this: Grid.BindingGroup.CancelEdit(). My question is, what if I have two Grids and use two BindingGroups one for each Grid, how can I refer to a specific BindingGroup?. – curzedpirate Oct 13 '12 at 04:13
  • If you set x:Name on the grid, VB will generate a friend variable of that name which holds a referende to the grid. So, you can use that in code behind. If you have several grids, just give them different names. – hbarck Oct 14 '12 at 10:04
1

The short answer is not good news - the isn't an equivalent method. Instead your approach needs to be a little different. One approach would be to implement the IEditableObject on your underlying class (or wrap it in an editable class). The second would be to implement Undo/Redo functionality. I think you will find searching for the terms bolded above and/or the memento pattern you will find lots of good examples, below is one.

Cancelling an update with WPF Binding

Community
  • 1
  • 1
Spevy
  • 1,325
  • 9
  • 22