djcouchycouch
djcouchycouch

Reputation: 12834

Removing event handlers in xaml style?

Is there a way to remove event handlers in a style that were defined in another style?

Here's a contrived example:

<Style TargetType="{x:Type TextBox}" x:Key="DefaultTextBoxStyle">
    <EventSetter Event="GotFocus" Handler="TextBox_GotFocus"/>
    <EventSetter Event="LostFocus" Handler="TextBox_LostFocus"/>
    <EventSetter Event="PreviewKeyUp" Handler="TextBox_PreviewKeyUp"/>
</Style>

<Style TargetType="{x:Type TextBox}" x:Key="InlineTextBox" BasedOn="{DynamicResource DefaultTextBoxStyle}">
    <EventSetter Event="GotFocus" Handler="????"/> // set to nothing
    <EventSetter Event="LostFocus" Handler="????"/> // set to nothing
    <EventSetter Event="PreviewKeyUp" Handler="????"/> // set to nothing
</Style>

Thanks!

Upvotes: 3

Views: 3705

Answers (1)

user7116
user7116

Reputation: 64068

From reading up on EventSetter, you'll have to have a dummy event which sets e.Handled. EventSetter states, "The event setter handlers from the style specified as BasedOn will be invoked after the handlers on the immediate style." So this would keep any EventSetter in your BasedOn from running unless it marked itself as HandledEventsToo.

<Style TargetType="{x:Type TextBox}" 
       x:Key="EatEvents"
       BasedOn="{StaticResource OtherStyle}">
  <EventSetter Event="Click" Handler="EatEventsHandler"/>
</Style>

public void EatEventsHandler(object sender, RoutedEventArgs e)
{
   e.Handled = true;
}

Upvotes: 2

Related Questions