Control.OnParentBindingContextChanged(EventArgs) 方法

定义

当控件容器的 BindingContextChanged 属性值更改时,将引发 BindingContext 事件。

C#
protected virtual void OnParentBindingContextChanged(EventArgs e);

参数

e
EventArgs

包含事件数据的 EventArgs

示例

下面的代码示例是属性值更改时 Text 执行的事件引发方法。 类Control具有多个名称模式 OnPropertyNameChanged 的方法,这些方法在 PropertyName 值更改时引发相应的 PropertyNameChanged 事件 (PropertyName 表示) 相应属性的名称。

下面的代码示例更改 ForeColor 显示货币数据的派生类的 TextBox 。 该示例将文本转换为十进制数,如果数字为负数,将 更改为 ForeColorColor.Red ;如果数字为正数,则更改为 Color.Black 。 此示例要求具有派生自 类的 TextBox 类。

C#
protected override void OnTextChanged(System.EventArgs e)
{
   try
   {
      // Convert the text to a Double and determine
      // if it is a negative number.
      if(double.Parse(this.Text) < 0)
      {
         // If the number is negative, display it in Red.
         this.ForeColor = Color.Red;
      }
      else
      {
         // If the number is not negative, display it in Black.
         this.ForeColor = Color.Black;
      }
   }
   catch
   {
      // If there is an error, display the 
      // text using the system colors.
      this.ForeColor = SystemColors.ControlText;
   }
   
   base.OnTextChanged(e);
}

注解

引发事件时,将通过委托调用事件处理程序。 有关详细信息,请参阅 处理和引发事件

方法 OnParentBindingContextChanged 还允许派生类在不附加委托的情况下处理事件。 这是在派生类中处理事件的首选技术。

继承者说明

在派生类中重写 OnParentBindingContextChanged(EventArgs) 时,一定要调用基类的 OnParentBindingContextChanged(EventArgs) 方法,以便已注册的委托对事件进行接收。

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

另请参阅