ns12345
ns12345

Reputation: 3105

WPF execute a function before a particular event occurs?

Is there a way to execute a piece of code before an event occurs?

example when we say Expanded="OnExpand" here the code inside OnExpand occurs after the Expanded event occurs. What if I want to execute a piece of code before that?

Upvotes: 1

Views: 480

Answers (3)

Wegged
Wegged

Reputation: 2413

You can use the Preview Events

A possible work around for the expander not having a PreviewExpanded event is to handle the PreviewMouseDown event and do a hit test to see if its on the Toggle Button.

Alternatively it may be possible to extend the Expander Class something along the lines of I did not test this at all no idea is it really works

public class MyExpander: Expander
{
    public event EventHandler<EventArgs> PreviewExpanded;

    public void OnPreviewExpanded()
    {
       PreviewExpanded(this,new EventArgs());
    }

    public override void OnExpanded()
    {
       PreviewExpanded()
       base.OnExpanded();
    }
}

Upvotes: 3

TheZenker
TheZenker

Reputation: 1730

If whatever object you are working with supports this behavior it will be in a matching "Preview" event. So, these two events are a before and after matched set. KeyDown() PreviewKeyDown()

Expander does not have a preview event for Expanded, if that is what you are working with.

Upvotes: 1

Chris Wenham
Chris Wenham

Reputation: 24017

If you're talking about the Expander control, you could subclass it and override the IsExpanded property, raising your own PreviewExpanded event before calling base.IsExpanded = value;

Upvotes: 2

Related Questions