开发者

WPF - Temporarily Stop Data Binding (databinding)

开发者 https://www.devze.com 2022-12-19 02:49 出处:网络
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.

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 entered a value (textbox lost focus) I want the textbox to return to display the bound value and not th开发者_StackOverflow中文版e value I have just entered manually.


I had the same problem and I solve it by

  1. With the BindingNavigator, I set it to null

    bdNavProduct.BindingSource =null;

  2. With other textbox control, datagridview, I clear the DataBinding

         txtProductID.DataBindings.Clear();
         txtProductName.DataBindings.Clear();
         txtQuantity.DataBindings.Clear();
         txtUnitPrice.DataBindings.Clear();
         dgvProduct.DataBindings.Clear();
    

Tony


You could attach to the text input event and cancel the binding and then reapply it on the lost focus event.

I think however you need to consider why you are displaying your bound value in a text box at all? Would it not be more appropriate to have an uneditable Textblock displaying your database information with the editable Textbox separate for the optional user input.

While what you are asking is doable using the Textbox event I mentioned, it seems like it would be confusing from a user perspective.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号