Control.OnParentBindingContextChanged(EventArgs) 方法

定义

当控件容器的 BindingContextChanged 属性值更改时,将引发 BindingContext 事件。Raises the BindingContextChanged event when the BindingContext property value of the control's container changes.

protected:
 virtual void OnParentBindingContextChanged(EventArgs ^ e);
protected virtual void OnParentBindingContextChanged (EventArgs e);
abstract member OnParentBindingContextChanged : EventArgs -> unit
override this.OnParentBindingContextChanged : EventArgs -> unit
Protected Overridable Sub OnParentBindingContextChanged (e As EventArgs)

参数

e
EventArgs

包含事件数据的 EventArgsAn EventArgs that contains the event data.

示例

下面的代码示例是一个事件引发方法,当 Text 属性值更改时,将执行该方法。The following code example is an event-raising method that is executed when the Text property value changes. Control 类具有多个方法,这些方法具有 name 模式 OnpropertynameChanged,可在propertyname值更改时引发相应的propertynameChanged 事件(PropertyName表示对应的属性的名称)。The Control class has several methods with the name pattern OnPropertyNameChanged that raise the corresponding PropertyNameChanged event when the PropertyName value changes (PropertyName represents the name of the corresponding property).

下面的代码示例更改了显示货币数据的 TextBox 派生类的 ForeColorThe following code example changes the ForeColor of a TextBox derived class displaying currency data. 该示例将文本转换为十进制数字,并在数字为负数时将 ForeColor 更改为 Color.Red,如果数字为正,则更改为 Color.BlackThe example converts the text to a decimal number and changes the ForeColor to Color.Red if the number is negative and to Color.Black if the number is positive. 此示例要求具有派生自 TextBox 类的类。This example requires that you have a class that derives from the TextBox class.

protected:
   virtual void OnTextChanged( System::EventArgs^ e ) override
   {
      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 ( Exception^ ) 
      {
         // If there is an error, display the
         // text using the system colors.
         this->ForeColor = SystemColors::ControlText;
      }

      TextBox::OnTextChanged( e );
   }
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);
}
Protected Overrides Sub OnTextChanged(e As System.EventArgs)
   Try
      ' Convert the text to a Double and determine
      ' if it is a negative number.
      If Double.Parse(Me.Text) < 0 Then
         ' If the number is negative, display it in Red.
         Me.ForeColor = Color.Red
      Else
         ' If the number is not negative, display it in Black.
         Me.ForeColor = Color.Black
      End If
   Catch
      ' If there is an error, display the
      ' text using the system colors.
      Me.ForeColor = SystemColors.ControlText
   End Try

   MyBase.OnTextChanged(e)
End Sub

注解

引发事件时,将通过委托调用事件处理程序。Raising an event invokes the event handler through a delegate. 有关详细信息,请参阅处理和引发事件For more information, see Handling and Raising Events.

利用 OnParentBindingContextChanged 方法,派生类还可以处理事件,而无需附加委托。The OnParentBindingContextChanged method also enables derived classes to handle the event without attaching a delegate. 这是在派生类中处理事件的首选技术。This is the preferred technique for handling the event in a derived class.

继承者说明

在派生类中重写 OnParentBindingContextChanged(EventArgs) 时,一定要调用基类的 OnParentBindingContextChanged(EventArgs) 方法,以便已注册的委托对事件进行接收。When overriding OnParentBindingContextChanged(EventArgs) in a derived class, be sure to call the base class's OnParentBindingContextChanged(EventArgs) method so that registered delegates receive the event.

适用于

另请参阅