The correct way to databound input dialogs ?

Hi,

I have read the guidelines to writing input dialogs from the MSDN :

http://msdn2.microsoft.com/en-us/library/aa969773.aspx

In one of the examples they build a dialog that can be used to add/edit a margin, which is a Thickness structure. The DataContext of the dialog is set the object to be edited, and the TextBoxes are databound to each field of the structure, with the UpdateSourceTrigger="PropertyChanged".

Then, when the dialog is closed with OK, the value can be copied back into the original object that was being modified, and if the dialog is close with Cancel, nothing is done, so the original object keeps its default values.

Now the problem I am facing is that I am using the same technique with an object that is a class, not a struct. So, the original object gets updated as soon as the user starts changing the values. In case the dialog is closed with Cancel, the object still gets updated.

I thought I might update the field with UpdateSourceTrigger="Explicit", so I only update them when we click OK. But then I have to update the source manually for every field, which kind of ruins the beauty of the original technique.

Is there any proper way to do this with a reference type ?

Thanks in advance for your thoughts.

Xavier

[3246 byte] By [xps] at [2008-1-10]
# 1

Unfortunately, I don’t think there’re any better solutions. If you don’t set UpdateSourceTrigger to Explicit, you can’t prevent the data source from being modified when a value is changed, or a TextBox loses focus…

Yi-LunLuo-MSFT at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 2
Maybe you should not use 2 way databinding if it will create problems for you.... DataBinding is nice because the notification system will notify the listener and update the source... in your case you want to update without having any notifications... you want to update only once when the ok button is clicked...

I would suggest that you just do one way databinding and in the button click event handler update the source .....

Regards

MarlonGrech at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 3

Thank you both for your input.

Well, I though that because this is a common senario, there would have been a better solution. The good thing with databinding it that if you ever have to add a field you just add the TextBox in the dialog, and don't have to change anything else.

If I choose to go with UpdateSourceTrigger="Explicit", is there still a way to update the whole DataContext in one time ? Of course I could still write a procedure that goes through the data and update each field...


Thanks

xps at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 4

have a look here....

http://www.beacosta.com/blog/?m=200512

regards

MarlonGrech at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...
# 5
Thanks Marlon
xps at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Presentation Foundation (WPF)...

Visual Studio Orcas

Site Classified