Intersoft ClientUI Documentation
How to: Add an Event Handler using Code

The following examples show how to add an event handler for a routed event by using code.

Example

Description

The controls in ClientUI generally have event fields that corresponds to each routed event available in the control. For example, the Click event field in UXButton control allows you to handle its corresponding Click routed event using language-specific operator.

The following code example shows how to add an event handler using C# operator syntax.

Code

XAML
Copy Code
<Grid x:Name="LayoutRoot">
        <StackPanel>
            <Intersoft:UXButton Name="button1" Click="button1_Click"
                                Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" />
        </StackPanel>
</Grid>
C#
Copy Code
void UXPage1_Loaded(object sender, RoutedEventArgs e)
{
    button1.Click += new RoutedEventHandler(button1_Click);
}

void button1_Click(object sender, RoutedEventArgs e)
{
    MessageBox.Show("Button1 Clicked");
}

Example 

Description 

The following code examples shows how to use the AddHandler method to handle the Click routed event of a UXButton.

Code

XAML
Copy Code
<Grid x:Name="LayoutRoot">
        <StackPanel>
            <Intersoft:UXButton Name="button1" Click="button1_Click"
                                Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" />
        </StackPanel>
</Grid>
C#
Copy Code
void UXPage1_Loaded(object sender, RoutedEventArgs e)
{
    button1.AddHandler(ISButton.ClickEvent, new RoutedEventHandler(button1_Click));
}

void button1_Click(object sender, RoutedEventArgs e)
{
    MessageBox.Show("Button1 Clicked");
}

Example

Description

Routed event introduces a concept whereby the event handler can be marked as handled to stop the event to be routed to the next node. This is done by setting the Handled property of the event data to true. 

ClientUI provides instance handlers with "handledEventsToo" mechanism whereby listeners can still run handlers in response to routed events where Handled is true in the event data. 

The following code examples shows how to use the AddHandler(RoutedEvent, Delegate, Boolean) method to handle a routed event that is invoked regardless of the Handled property value.

Please note an import to Intersoft.Client.Framework is required to use the AddHandler extension method. For instance, the reference import in C# code is: using Intersoft.Client.Framework.

Code

C#
Copy Code
void UXPage1_Loaded(object sender, RoutedEventArgs e)
{
    button1.AddHandler(ISButton.ClickEvent, new RoutedEventHandler(button1_Click), true);
}

void button1_Click(object sender, RoutedEventArgs e)
{
    MessageBox.Show("Button1 Clicked");
}

Example

Description

In addition to the AddHandler(RoutedEvent, Delegate, Boolean) extension method, you can also use the static method ISEventManager.RegisterInstanceHandler to handle a routed event that is invoked regardless of the Handled property value.

Code

C#
Copy Code
void UXPage1_Loaded(object sender, RoutedEventArgs e)
{
    ISEventManager.RegisterInstanceHandler(button1, UXButton.ClickEvent, new RoutedEventHandler(button1_Click), true);
}

void button1_Click(object sender, RoutedEventArgs e)
{
    MessageBox.Show("Button1 Clicked");
}
See Also

Concepts

Other Resources