Reputation: 3105
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
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
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
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