Skip to main content
.NET Framework Class Library
EventSetter Class

Represents an event setter in a style. Event setters invoke the specified event handlers in response to events.

Namespace:   System.Windows
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation
Syntax
Public Class EventSetter _
	Inherits [%$TOPIC/ms602396_en-us_VS_110_2_0_0_0_0%]
public class EventSetter : [%$TOPIC/ms602396_en-us_VS_110_2_0_1_0_0%]
public ref class EventSetter : public [%$TOPIC/ms602396_en-us_VS_110_2_0_2_0_0%]
type EventSetter =  
    class 
        inherit [%$TOPIC/ms602396_en-us_VS_110_2_0_3_0_0%] 
    end
public class EventSetter extends [%$TOPIC/ms602396_en-us_VS_110_2_0_4_0_0%]
<EventSetter .../>

The EventSetter type exposes the following members.

Constructors
  NameDescription
Public method EventSetterInitializes a new instance of the EventSetter class.
Public method EventSetter(RoutedEvent, Delegate)Initializes a new instance of the EventSetter class, using the provided event and handler parameters.
Top
Properties
  NameDescription
Public property EventGets or sets the particular routed event that this EventSetter responds to.
Public property HandledEventsTooGets or sets a value that determines whether the handler assigned to the setter should still be invoked, even if the event is marked handled in its event data.
Public property HandlerGets or sets the reference to a handler for a routed event in the setter.
Public property IsSealedGets a value that indicates whether this object is in an immutable state. (Inherited from SetterBase.)
Top
Methods
  NameDescription
Protected method CheckSealedChecks whether this object is read-only and cannot be changed. (Inherited from SetterBase.)
Public method Equals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected method FinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCodeServes as the default hash function. (Inherited from Object.)
Public method GetTypeGets the Type of the current instance. (Inherited from Object.)
Protected method MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public method ToStringReturns a string that represents the current object. (Inherited from Object.)
Top
Remarks

Event setters invoke the specified event handlers in response to routed events, which apply to all elements that reference the Style rather than requiring you to attach instance handlers to each individual element. Only StyleSetters support EventSetter objects.

You can only declare event setters for events that are routed events. You can declare event setters for routed events with a direct routing strategy, bubbling events, or tunneling events.

Instances of the EventSetter class are usually created through Extensible Application Markup Language (XAML), as an object element within a style defined through XAML. An EventSetter might also be instantiated from code.

Event setters cannot be used in a style that is contained in a theme resource dictionary. This is because a theme resource dictionary at run time is often loose binary XAML (BAML) files, and does not have any scope defined where accompanying code-behind that defines the handlers can exist.

Handlers attached through event setters are invoked after any class handlers for an event, and also after any instance handlers. As a result, if a class handler or instance handler marks an event handled in its arguments, then the handler declared by an event setter is not invoked, unless the event setter specifically sets HandledEventsToo true.

Event setters may also come from BasedOn styles. The event setter handlers from the style specified as BasedOn will be invoked after the handlers on the immediate style.

Note that only StyleSetters supports EventSetter objects. Triggers ( TriggerBase and derived classes) do not support EventSetter.

Examples

The following example establishes a single EventSetter within a page-level style.

<StackPanel
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Class="SDKSample.EventOvw2"
  Name="dpanel2"
  Initialized="PrimeHandledToo"
>
  <StackPanel.Resources>
    <Style TargetType="{x:Type Button}">
      <EventSetter Event="Click" Handler="b1SetColor"/>
    </Style>
  </StackPanel.Resources>
  <Button>Click me</Button>
  <Button Name="ThisButton" Click="HandleThis">
    Raise event, handle it, use handled=true handler to get it anyway.
  </Button>
</StackPanel>

The following shows the example event handlers:

	  Private Sub b1SetColor(ByVal sender As Object, ByVal e As RoutedEventArgs)
		Dim b As Button = TryCast(e.Source, Button)
		b.Background = New SolidColorBrush(Colors.Azure)
	  End Sub

	  Private Sub HandleThis(ByVal sender As Object, ByVal e As RoutedEventArgs)
		e.Handled=True
	  End Sub
void b1SetColor(object sender, RoutedEventArgs e)
{
  Button b = e.Source as Button;
  b.Background = new SolidColorBrush(Colors.Azure);
}

void HandleThis(object sender, RoutedEventArgs e)
{
  e.Handled=true;
}
Version Information

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.