locked
Assign attached events using style and EventSetter RRS feed

  • Question

  • Hi guys,

    I would like to assign a handler to attached event for given element using style and EventSetter. Following example demonstrates the idea.

    <Window.Resources> <Style x:Key="keyStyle" TargetType="FrameworkElement"> <EventSetter Event="Binding.TargetUpdatedEvent" Handler="OnTragetUpdated"/> </Style> </Window.Resources>

    That fails to compile. In that particular case my problem is that corresponding direct event for the FrameworkElement event TargetUpdated is not RoutedEvent and thus can not be set using Style and EventSetter although it can be set directly for the element.

    Could anyone tell me if there is a way to assign event handler to TargetUpdated event directly in XAML or I should do that in code?
    Thursday, June 25, 2009 8:26 AM

Answers

All replies

    • Marked as answer by Hua Chen Thursday, July 2, 2009 8:28 AM
    Thursday, June 25, 2009 9:26 AM
  • Hi Boris Dachev,

    You can only declare an eventsetter for events that are routed events. Here the TargetUpdated event of FrameworkElement is not a routed event. So you can't use it in EventSetter . You have to do that in code.
    Thursday, June 25, 2009 9:33 AM
  • Hello Boris,

      Please try the following syntax:

    <Window.Resources>
            <Style x:Key="keyStyle" TargetType="FrameworkElement">
                <EventSetter Event="Binding.TargetUpdated" Handler="HandleUpBinding"></EventSetter>
            </Style>
     </Window.Resources>
     Please remove the Event fix.

      Thanks.
    Please mark the replies as answers if they help and unmark them if they provide no help
    • Marked as answer by Hua Chen Thursday, July 2, 2009 8:28 AM
    Thursday, July 2, 2009 8:28 AM