C# WinForm 自定义控件绑定属性DataBindings
https://www.cnblogs.com/jizhongfong/p/4384689.html
var bind = new Binding("Enabled", OrderViewModel.Instance.Commands.ThreadManager, "IsRunning");
bind.Format += delegate(object o, ConvertEventArgs args) { args.Value = !((bool) args.Value); };
button1.DataBindings.Add(bind);
var bind = new Binding("Text", Query, "IsValid");
bind.Format += delegate (object o, ConvertEventArgs args)
{
if (Query.WhereIsValid)
{
args.Value = ((bool)args.Value ? "是" : "否");
}
else
{
args.Value = "[不限-All]";
}
};
com_Valid.DataBindings.Add(bind);
/// <summary> /// 将绑定数据源和控件绑定 /// </summary> /// <param name="ctl">控件</param> /// <param name="propertyName">属性名称</param> /// <param name="BindingSource">数据源</param> /// <param name="fieldName">绑定字段</param> public static void SetDataBinding(this System.Windows.Forms.Control ctl, string propertyName, object BindingSource, string fieldName) { if (ctl.DataBindings[propertyName] != null) { ctl.DataBindings.Remove(ctl.DataBindings[propertyName]); } ctl.DataBindings.Add(propertyName, BindingSource, fieldName, true, DataSourceUpdateMode.OnPropertyChanged); } public static void RemoveDataBinding(this System.Windows.Forms.Control ctl, string propertyName) { if (ctl.DataBindings[propertyName] != null) { ctl.DataBindings.Remove(ctl.DataBindings[propertyName]); } }