码迷,mamicode.com
首页 > Windows程序 > 详细

C# WinForm 自定义控件绑定属性DataBindings

时间:2021-06-25 17:22:34      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:property   inf   string   控件   class   ali   col   eve   http   

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]); }
        }

 







C# WinForm 自定义控件绑定属性DataBindings

标签:property   inf   string   控件   class   ali   col   eve   http   

原文地址:https://www.cnblogs.com/LuoCore/p/14930568.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!