C# 自定义控件摘记
C# 自定义控件属性
现有自定义控件,内有一textbox控件 TextBox1。控件有一属性 Value 定义为 [BrowsableAttribute(true)] [BindableAttribute(true)] public string Value { get{return TextBox1.Text;} set{TextBox1.Text =value;} } 将该控件的实例绑定到窗体的bind...展开
跟踪了 当bindingSource.Current发生变更时,自定义属性和控件的普通控件属性的区别:
自定义的属性:会触发2次BindingComplete事件 一次是BindingCompleteContext.ControlUpdate 控件把值更新到变更前的bindingSource行
第二次是BindingCompleteContext.DataSourceUpdate,bindingSource的新行把数据给控件
普通控件属性:只触发后面一次
定义 DataBindings
https://www.daniweb.com/programming/software-development/threads/255512/binding-an-application-setting-to-a-control-property
class BindableToolStripMenuItem : ToolStripMenuItem,IBindableComponent { #region IBindableComponent Members private BindingContext bindingContext; private ControlBindingsCollection dataBindings; [Browsable(false)] public BindingContext BindingContext { get { if (bindingContext == null) { bindingContext = new BindingContext(); } return bindingContext; } set { bindingContext = value; } } [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] public ControlBindingsCollection DataBindings { get { if (dataBindings == null) { dataBindings = new ControlBindingsCollection(this); } return dataBindings; } } #endregion }
PropertyChanged;
/// <summary> /// 外部修改ResultId值可以通知邦定的控件,内部修改无法通知邦定的控件??????? /// </summary> [Description("属性:返回ID")] //[Bindable(true)] [BrowsableAttribute(true)] [System.ComponentModel.Bindable(true)] public string ResultId { get {//InvokePropertyChanged("ResultId"); return _ResultId; } set { _ResultId = value; InvokePropertyChanged("ResultId"); } } public event PropertyChangedEventHandler PropertyChanged; private void InvokePropertyChanged(string propName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propName)); }
https://documentation.devexpress.com/#WindowsForms/CustomDocument113956/baseclass
https://www.youtube.com/watch?v=U1eypjtM8JI