开发者

WPF execute a function before a particular event occurs?

开发者 https://www.devze.com 2023-02-15 23:54 出处:网络
Is there a w开发者_C百科ay to execute a piece of code before an event occurs? example when we say Expanded=\"OnExpand\"

Is there a w开发者_C百科ay 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?


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();
    }
}


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;


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消