如何在ControlBindingCollection中找到特定的绑定? [英] How do I find a particular binding in ControlBindingCollection?
本文介绍了如何在ControlBindingCollection中找到特定的绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试向代码中添加AlreadyBound方法,以便避免错误
I am trying to add an AlreadyBound method to my code so that I can avoid an error
HResult=0x80070057 This causes two bindings in the collection to bind to the same property.
由
System.Windows.Forms.ControlBindingsCollection.CheckDuplicates(Binding binding)
我的代码是
public static void BindText(TextBox box, object dataSource, string dataMember)
{
if (AlreadyBound(box,"Text")) return;
box.DataBindings.Add("Text", dataSource, dataMember, true, DataSourceUpdateMode.OnPropertyChanged);
if (!(box is SnapTextBox snapbox)) return;
switch (snapbox.SnapType)
{
case SnapBoxType.Money:
snapbox.DataBindings[0].FormatString = "N2";
break;
case SnapBoxType.Real:
snapbox.DataBindings[0].FormatString = $"N{snapbox.SnapRealDecimals}";
break;
case SnapBoxType.Text:
break;
case SnapBoxType.Integer:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
private static bool AlreadyBound(TextBox box, string propertyName)
{
foreach (var binding in box.DataBindings)
{
// what do I put here? something like
//if (binding.ToString() == propertyName) return true;
}
return false;
}
其中SnapTextBox是用户定义的控件。
Where SnapTextBox is a user defined control.
如何使AlreadyBound工作?
How do I get AlreadyBound to work?
推荐答案
我在弄清楚应该使用哪种类型绑定时遇到了麻烦声明为。
最后,我尝试查看box.DataBindings [0]
I had trouble figuring out what type binding should be declared as. Finally I tried looking at the type for box.DataBindings[0]
private static bool AlreadyBound(TextBox box, string propertyName)
{
foreach (Binding binding in box.DataBindings)
{
if (binding.PropertyName == propertyName) return true;
}
return false;
}
这篇关于如何在ControlBindingCollection中找到特定的绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文