Michael Haddad
Michael Haddad

Reputation: 4455

In WPF, how to know when an image has moved (due to another element Visibility changed to "Collapsed")?

I have a StackPanel and an Image. When the user clicks a button, the StackPanel Visibility property is changing to Collapsed, making the image to change location.

Is there an event for that scenario? Another way to know when it is happening?

Upvotes: 0

Views: 27

Answers (1)

Blinx
Blinx

Reputation: 400

Presuming that you are using a binding to change the Visibility of the StackPanel you could change the setter of the binding source to call a method on change:

Visibility vis;

public Visibility Vis
{
    get { return vis; }
    set
    {
        vis = value;
        imageLocationChanged();
        NotifyPropertyChanged("Vis");
    }
}

void imageLocationChanged()
{
    //Do stuff
}

Note: If you have multiple bindings that may influence the images position, you would have to call this method from each setter

EDIT (reflecting OP's comment):

If setting the visibility in the code behind without binding then just call the method you need after setting the visibility:

stackpanel1.Visibility = Visibility.Collapsed;
imageLocationChanged();

Upvotes: 1

Related Questions