DFGUI additionally has the concept of “event bubbling”. When a control fires an event, this event can be handled by a script on the control. If the event is not consumed by a script on the control (by calling the Use() method of the passed EventArgs), it is forwarded to the parent of the control, all the way up the hierarchy.
There are three main ways to handle events. You can either use an Event Binding to bind the control event to a method in your script, you can handle them SendMessage-style (with a specially named method - pattern is On[EventName], so for instance OnClick will be called upon the Click event), or you can subscribe to the event with standard .NET event handlers (such as “control.Click += control_OnClick”).
So, there are 3 ways to add Event Handler to control, such as a button control:
1. create new script with handler function (such as OnClick), and binding the script to the control,.there are 2 way to bind, one is dragging the script from the Asset window to the Hierarchy Window, two is and new script as a component.
2. Add Df Event Binding to the control,
Event Binding
To add an event binding to a control, right click the control and choose Add Binding → Event Binding.
You can assign the Source and Target components the same way you would a Property Binding component. Choose the event of the Data Source, and the function to call on the Data Target. Keep in mind that the target function must either be parameterless, or match the signature of the bound event. IEnumerators are supported as target functions, in which case the function is started as a coroutine.
3. delegate
when creating a control dynamic , delegating is a good choice,
var button = target.AddControl<dfButton>();
// Set whatever properties the control needs after creation
button.NormalBackgroundColor = colors[ colorNum % colors.Length ];
button.BackgroundSprite = "button-normal";
button.Text = string.Format( "Button {0}", button.ZOrder );
button.Anchor = dfAnchorStyle.Left | dfAnchorStyle.Right;
button.Width = target.Width - target.ScrollPadding.horizontal;
//event handler
button.Click += ButtonClick;
void ButtonClick(dfControl control, dfMouseEventArgs mouseEvent)
{
Debug.Log(((dfButton)control).Text + " clicked");
}