WPF-以编程方式暂停数据绑定 [英] WPF - Programmaticaly Pause Databinding

查看:92
本文介绍了WPF-以编程方式暂停数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框,该文本框(单向)绑定到一个数据表,该数据表每秒更新两次.因此,文本框值会不断反映数据表中的更改.当我输入文本框以手动设置值时,绑定将导致该值不断被覆盖.如何停止呢?输入值(文本框失去焦点)后,我希望文本框返回以显示绑定值,而不是我刚刚手动输入的值.

请注意,该文本框是众多文本框之一,每个文本框都附加到DataTable中的一个字段.我不希望数据表在更改一个文本框时停止向其他文本框发送更新.请注意,这是OneWay绑定.
我不相信可以阻止对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 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?

推荐答案

将BindingMode设置为OneWay.实现INotifyPropertyChanged.在TextBoxValue属性的设置器中,仅当textBox1.IsFocused为false时才发出更改通知.然后,在textBox1的LostFocus事件上,将TextBoxValue更新为其自身(假设IsFocused在那时为false,应该发送通知),或者从事件处理程序手动发送通知,而无需实际设置TextBoxValue.当实现INotifyPropertyChanged时,仅在代码发送通知时才执行绑定(通常在属性设置器中).

如果需要双向绑定,则可以创建两个属性. textBox1绑定到的一个属性.以及由数据库更新的其他属性.设置TextBoxValue时,还会将TextBoxValueSetDate设置为DateTime.Now.设置DatabaseValue时,还会将DatabaseValueSetDate设置为DateTime.Now.在TextBoxValue的getter中,它仍然具有仅通知IsFocused是否为false的逻辑,但它还将根据新的日期(TextBoxValueSetDate或DatabaseValueSetDate)选择要返回的值.
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).


这篇关于WPF-以编程方式暂停数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆