Winforms 应用程序中的 2 路绑定使得在 TextBox 中输入文本变得非常慢
我已经在我的表单(它有 32 个控件)和我的类的实例之间设置了 2 路绑定,但是在 TextBox
中输入的每个字符都有 1/2 秒的延迟,这使得应用程序几乎无法使用。
当我使用 DataSourceUpdateMode.Never 时,问题不会发生,这清楚地表明 2 路绑定是罪魁祸首。
请注意,如果我为每个控件but设置DataSourceUpdateMode.Never
,则该控件存在滞后,因此似乎不是导致绑定控件的数量的问题。
parameterTagRecord = new PETParameterTagRecord(TagID);
baseTagNameTB.DataBindings.Add("Text", parameterTagRecord,
"BaseTagName", true, DataSourceUpdateMode.OnPropertyChanged);
我的课堂摘录:
public class PETParameterTagRecord : PETBaseObject, INotifyPropertyChanged
{
private string _baseTagName = Constants.NullString;
public event PropertyChangedEventHandler PropertyChanged;
public string BaseTagName
{
get { return _baseTagName; }
set
{
_baseTagName = value;
NotifyPropertyChanged("BaseTagName");
}
}
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
我做错了什么?
I've set-up 2-way binding between my form (it has 32 controls) and an instance of my class but each character entered in a TextBox
has that 1/2 second delay which makes the application almost unusable.
When I use DataSourceUpdateMode.Never
, the problem does not occur which clearly indicates the 2-way binding is the culprit.
Note that if I set DataSourceUpdateMode.Never
for each control but one, the lag exists for that one control so it doesn't seem to be the number of bound controls that causes the issue.
parameterTagRecord = new PETParameterTagRecord(TagID);
baseTagNameTB.DataBindings.Add("Text", parameterTagRecord,
"BaseTagName", true, DataSourceUpdateMode.OnPropertyChanged);
And an extract of my class:
public class PETParameterTagRecord : PETBaseObject, INotifyPropertyChanged
{
private string _baseTagName = Constants.NullString;
public event PropertyChangedEventHandler PropertyChanged;
public string BaseTagName
{
get { return _baseTagName; }
set
{
_baseTagName = value;
NotifyPropertyChanged("BaseTagName");
}
}
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
}
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它不应该那么慢,但是有一个选项可以让您在按键或失去焦点时更改文本框。尝试将其设置为失去焦点。另外,在设置器中,请务必在设置和引发事件之前检查 _baseTagName != 值。这也会减慢速度。
因此,首先尝试像这样更改绑定:
请参阅此 MSDN 链接: http://msdn.microsoft.com/en-us/library/system.windows.forms.datasourceupdatemode.aspx。这意味着,不是每次按键都会导致新的字符串值被推入属性中,而是只会在验证时执行此操作(这作为控件失去焦点的一部分而发生)。
其次,更改您的属性实现以匹配此内容:
现在,无论属性是否实际更改,您都将引发事件。这也不利于性能。
It shouldn't be that slow, but there's an option where you can have the textbox change on key press or on lost focus. Try setting it to lost focus. Also in your setter, be sure to check that _baseTagName != value before setting and raising the event. That will slow things up a bunch as well.
So, first try changing your binding like this:
See this MSDN link: http://msdn.microsoft.com/en-us/library/system.windows.forms.datasourceupdatemode.aspx. This means that instead of every keypress causing the new string value to be pushed into the property, it will only do so on Validation (which happens as part of the control losing focus).
Second, change your property implementation to match this:
Right now you're raising the event whether the property has actually changed or not. That is also detrimental to performance.
我在 BindingSource 中遇到了同样的问题。这与更新模式或过于频繁地触发通知无关(尽管是间接的)。当前的实现会导致每个单个绑定元素在任何属性发生更改时刷新。因此,OnValidation 不那么严重的原因是显而易见的,它发生的频率较低。
也很容易检查。添加两个计数器,每当访问 getter 或调用 NotifyProperChanged 时,每个计数器都会增加。就我而言,大约有 40 个元素,加载表单后我的进度为 1/40。在文本框中添加一个字符,突然在 2/80 处。按住该键,我的应用程序停止响应。一旦它最终追上来,计数就会达到 50/2000 之类的荒谬水平。一切都源于一个元素的改变。
我可能是错的,但我不明白这有什么意义或可能是所需的实现。为什么当一个元素发生变化时我要更新整个表单,这违背了绑定特定元素的初衷。
I ran into the same exact issue with BindingSource. This has has nothing to do with the update mode or notifications being fired too often (though indirectly, it does). The current implementation causes every single bound element to refresh whenever any property changes. So the reason OnValidation is less of an issue is obvious, it happens less frequently.
Fairly easy to check, too. Add two counters, increase each whenever a getter is accessed or NotifyProperChanged is called. In my case, with roughly 40 elements, I'd be at 1/40 after loading the form. Add a character in a textbox, suddenly at 2/80. Keeping the key pressed, my app stopped being responsive. Once it finally caught up, the count stood at something ridiculous like 50/2000. All from one single element changing.
I might be wrong, but I don't see how this makes sense or could be the desired implementation. Why would I want to update the whole form when one element changes, defeats the point of binding specific elements in the first place.