tags:

views:

654

answers:

1

Hi,

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!

+1  A: 

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;
}
sixlettervariables
won't work... it would be like doing `this.GotFocus = null`, which is not legal. You can only access an event through += and -=
Thomas Levesque
Reading...turns out to be fundamental :D
sixlettervariables
Thanks! Seems like the best solution for now.
Steve the Plant