1

Possible Duplicate:
Unsubscribe anonymous method in C#
Single-shot event subscription

Is it possible to get a reference to an event handler from within the event handler itself so you can unhook it from the event that called it?

For instance, I'd love to make the control.Loaded event point to a Lambda, but if I did, I don't know what to pass to the unhook (-=) call.

Here's an excerpt of the code:

private static void IsEnabled_Changed(object sender, DependencyPropertyChangedEventArgs e)
{
    var control = (Control)sender;

    if(control.IsLoaded)
        WireUpScrollViewerEvents(control);
    else
        control.Loaded += Control_Loaded;

}

private static void Control_Loaded(object sender, RoutedEventArgs e)
{
    var control = (Control)sender;

    control.Loaded -= Control_Loaded; // <-- How can I do this from a lambda in IsEnabled_Changed?

    WireUpScrollViewerEvents(control);
}

private static void WireUpScrollViewerEvents(Control control, bool attach)
{
    var scrollViewer = Utils.FindFirstVisualChild<ScrollViewer>(control);
    if(scrollViewer == null) return;

    var attachEvents = GetIsEnabled(control);

    if(attachEvents)
    {
        scrollViewer.PreviewDragEnter += ScrollViewer_PreviewDragEnter;
        scrollViewer.PreviewDragOver  += PreviewDragOver;
    }
    else
    {
        scrollViewer.PreviewDragEnter -= ScrollViewer_PreviewDragEnter;
        scrollViewer.PreviewDragOver  -= PreviewDragOver;
    }
}

The only reason Control_Loaded is an actual method is because of the 'control.Loaded -= Control_Loaded' line. I’m wondering if we can anonymous-lambda away it from within the IsEnabled_Changed call directly.

Community
  • 1
  • 1
Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286
  • 1
    Aaah! I was searching for events and didn't see that. Not quite the same though as mine takes parameters while theirs is just an Action. What would the syntax for mine be? – Mark A. Donohoe Sep 13 '12 at 17:57
  • @MarqueIV You would use one of the various generic versions of `Action` with the type of each parameter listed; beyond that it is identical. – Servy Sep 13 '12 at 18:05
  • @spender, actually, that isn't the same thing because the outer assignment of the variable that holds the anonymous method isn't yet assigned so you can't use it inside to unsubscribe. – Mark A. Donohoe Sep 13 '12 at 18:13

1 Answers1

2

Ok, found it.

Ok, found it. The reason I couldn't get it to work was I was doing this...

RoutedEventHandler Control_Loaded = (s2, e2) =>
{
    control.Loaded -= Control_Loaded;
    WireUpScrollViewerEvents(control);
};

...when I should have been doing this...

RoutedEventHandler Control_Loaded = null; // <-- It's now declared before it's set, which is what we need
Control_Loaded = (s2, e2) =>
{
    control.Loaded -= Control_Loaded;
    WireUpScrollViewerEvents(control);
};

This is because you have to actually declare the variable before you can use it, and I was trying to use it as part of the declaration. This fixes that.

Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286