Share via


XamlDirect.AddEventHandler Method

Definition

Overloads

AddEventHandler(IXamlDirectObject, XamlEventIndex, Object)

Adds the specified event handler for a specified event using XamlEventIndex, adding the handler to the handler collection on the current IXamlDirectObject.

AddEventHandler(IXamlDirectObject, XamlEventIndex, Object, Boolean)

Adds the specified event handler for a specified event using XamlEventIndex, adding the handler to the handler collection on the current IXamlDirectObject. Specify handledEventsToo as true to have the provided handler be invoked even if the event is handled elsewhere.

AddEventHandler(IXamlDirectObject, XamlEventIndex, Object)

Adds the specified event handler for a specified event using XamlEventIndex, adding the handler to the handler collection on the current IXamlDirectObject.

public:
 virtual void AddEventHandler(IXamlDirectObject ^ xamlDirectObject, XamlEventIndex eventIndex, Platform::Object ^ handler) = AddEventHandler;
/// [Windows.Foundation.Metadata.DefaultOverload]
/// [Windows.Foundation.Metadata.Overload("AddEventHandler")]
void AddEventHandler(IXamlDirectObject const& xamlDirectObject, XamlEventIndex const& eventIndex, IInspectable const& handler);
[Windows.Foundation.Metadata.DefaultOverload]
[Windows.Foundation.Metadata.Overload("AddEventHandler")]
public void AddEventHandler(IXamlDirectObject xamlDirectObject, XamlEventIndex eventIndex, object handler);
function addEventHandler(xamlDirectObject, eventIndex, handler)
Public Sub AddEventHandler (xamlDirectObject As IXamlDirectObject, eventIndex As XamlEventIndex, handler As Object)

Parameters

xamlDirectObject
IXamlDirectObject

A reference to the current IXamlDirectObject.

eventIndex
XamlEventIndex

An identifier for the event to be handled specified through XamlEventIndex enum.

handler
Object

Platform::Object

IInspectable

A reference to the specified handler implementation.

Attributes

Examples

The following example shows how to add/modify the PointerEntered event through a specific instance of PointerEventHandler on a ToggleSwitch control from its IXamlDirectObject instance.

XamlDirect xd = XamlDirect.GetDefault();

IXamlDirectObject toggleSwitch = xd.CreateInstance(XamlTypeIndex.ToggleSwitch);

PointerEventHandler toggleSwitchPointerHandler = new PointerEventHandler((sender, args) =>
{
    if (sender is ToggleSwitch)
    {
        ((ToggleSwitch)sender).IsOn = !((ToggleSwitch)sender).IsOn;
    }
});

xd.AddEventHandler(toggleSwitch, XamlEventIndex.UIElement_PointerEntered, toggleSwitchPointerHandler);
XamlDirect^ xd = XamlDirect::GetDefault();

IXamlDirectObject^ toggleSwitch = xd->CreateInstance(XamlTypeIndex::ToggleSwitch);

PointerEventHandler^ toggleSwitchPointerHandler = ref new PointerEventHandler([&](Platform::Object^ sender, PointerRoutedEventArgs^ args)
{
    ToggleSwitch^ ts = dynamic_cast<ToggleSwitch^>(sender);
    if (nullptr != ts)
    {
        ts->IsOn = !ts->IsOn;
    }
});

xd->AddEventHandler(toggleSwitch, XamlEventIndex::UIElement_PointerEntered, toggleSwitchPointerHandler);

Remarks

AddEventHandler can only be used to add event handlers for the events supported by the XamlEventIndex enumeration. You can use this method to add handlers to routed as well non-routed events supported by the xaml object.

See also

Applies to

AddEventHandler(IXamlDirectObject, XamlEventIndex, Object, Boolean)

Adds the specified event handler for a specified event using XamlEventIndex, adding the handler to the handler collection on the current IXamlDirectObject. Specify handledEventsToo as true to have the provided handler be invoked even if the event is handled elsewhere.

public:
 virtual void AddEventHandler(IXamlDirectObject ^ xamlDirectObject, XamlEventIndex eventIndex, Platform::Object ^ handler, bool handledEventsToo) = AddEventHandler;
/// [Windows.Foundation.Metadata.Overload("AddEventHandler_HandledEventsToo")]
void AddEventHandler(IXamlDirectObject const& xamlDirectObject, XamlEventIndex const& eventIndex, IInspectable const& handler, bool const& handledEventsToo);
[Windows.Foundation.Metadata.Overload("AddEventHandler_HandledEventsToo")]
public void AddEventHandler(IXamlDirectObject xamlDirectObject, XamlEventIndex eventIndex, object handler, bool handledEventsToo);
function addEventHandler(xamlDirectObject, eventIndex, handler, handledEventsToo)
Public Sub AddEventHandler (xamlDirectObject As IXamlDirectObject, eventIndex As XamlEventIndex, handler As Object, handledEventsToo As Boolean)

Parameters

xamlDirectObject
IXamlDirectObject

A reference to the current IXamlDirectObject.

eventIndex
XamlEventIndex

An identifier for the event to be handled specified through XamlEventIndex enum.

handler
Object

Platform::Object

IInspectable

A reference to the specified handler implementation.

handledEventsToo
Boolean

bool

true to register the handler such that it is invoked even when the routed event is marked handled in its event data. false to register the handler with the default condition that it will not be invoked if the routed event is already marked handled.

The default is false and the parameter is optional.

Attributes

Examples

The following example shows how to add/modify the PointerEntered event through a specific instance of PointerEventHandler on a ToggleSwitch control from its IXamlDirectObject instance.

XamlDirect xd = XamlDirect.GetDefault();

IXamlDirectObject toggleSwitch = xd.CreateInstance(XamlTypeIndex.ToggleSwitch);

PointerEventHandler toggleSwitchPointerHandler = new PointerEventHandler((sender, args) =>
{
    if (sender is ToggleSwitch)
    {
        ((ToggleSwitch)sender).IsOn = !((ToggleSwitch)sender).IsOn;
    }
});

xd.AddEventHandler(toggleSwitch, XamlEventIndex.UIElement_PointerEntered, toggleSwitchPointerHandler, true);
XamlDirect^ xd = XamlDirect::GetDefault();

IXamlDirectObject^ toggleSwitch = xd->CreateInstance(XamlTypeIndex::ToggleSwitch);

PointerEventHandler^ toggleSwitchPointerHandler = ref new PointerEventHandler([&](Platform::Object^ sender, PointerRoutedEventArgs^ args)
{
    ToggleSwitch^ ts = dynamic_cast<ToggleSwitch^>(sender);
    if (nullptr != ts)
    {
        ts->IsOn = !ts->IsOn;
    }
});

xd->AddEventHandler(toggleSwitch, XamlEventIndex::UIElement_PointerEntered, toggleSwitchPointerHandler, true);

Remarks

AddEventHandler can only be used to add event handlers for the events supported by the XamlEventIndex enumeration. You can use this method to add handlers to routed as well non-routed events supported by the xaml object.

See UIElement.AddHandler for when to use handledEventsToo and the restrictions around the same. For non-routed events, the handleEventsToo flag is completely ignored.

See also

Applies to