Click here to Skip to main content
15,914,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a textbox that is bound (oneway) to a datatable that updates a couple of time a second. So the textbox value continually reflects changes in datatable. When I enter the textbox to manually set a value, the binding causes the value to continually be overwritten. How do I stop this? When I have entred a value (textbox lost focus) I want the textbox to return to display the bound value and not the value I have just entered manually.

Plase note that the textbox is one of many ... each attached to a field in a DataTable. I do not want the datatable to stop sending updates to the other textboxes while I alter one textbox. Note it is OneWay binding.
I don't believe I can prevent update on a single field in a DataTable.

Also is it not possile to remove binding and reset it programatically .. rather than controlling the sourse that is updating?
Posted
Updated 29-Jan-10 22:50pm
v3

1 solution

Set the BindingMode as OneWay. Implement INotifyPropertyChanged. In your setter for the TextBoxValue property, only send out a notification of the change if textBox1.IsFocused is false. Then, on your LostFocus event for textBox1, update TextBoxValue to itself (which, assuming IsFocused is false at that point, should send out a notification), or send out the notification manually from your event handler without actually setting the TextBoxValue. When you implement INotifyPropertyChanged, binding is only performed when a notification is sent out by your code (usually in the property setter).

If you need two-way binding, you could create two properties. One property that textBox1 is bound to. And the other property that gets updated by the database. When TextBoxValue gets set, it also sets TextBoxValueSetDate to DateTime.Now. When the DatabaseValue gets set, it also sets DatabaseValueSetDate to DateTime.Now. In the getter for TextBoxValue, it will still have the logic to only notify if IsFocused is false, but it will also then pick the value to return based on which date is newer (TextBoxValueSetDate or DatabaseValueSetDate).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900