Home > OS >  Xaml onVisibilityChanged do something
Xaml onVisibilityChanged do something

Time:09-13

I have element with visibility but I don't know to do something on isVisibleChanged right way.

So lets say I have element with name SomePanel
So I can access SomePanel.IsVisibleChanged

What I found on the internet is

public event DependencyPropertyChangedEventHandler IsSomePanelVisible;
somePanel.IsVisibleChanged  = IsSomePanelVisible;

But I need method after = where can I put my logic. How can I do that?

CodePudding user response:

First you have to create the method that will be called :

 private void VisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
    {

    }

Then add the handler :

somePanel.IsVisibleChanged  = VisibleChanged;

The method can be later in the code, it doesn't matter.

EDIT :

Like Shemberle said, you can also do it from the XAML like this : (expecting that you keep "VisibleChanged" as your method name)

<Panel IsVisibleChanged="VisibleChanged" />

The interesting point to note is that adding it in the XAML creates the "link" between the event and the method from the beginning whereas adding it programatically only creates the link at the moment of your choice. Conversely, you can remove the link in the same way, just change by - :

somePanel.IsVisibleChanged -= VisibleChanged;

CodePudding user response:

Eva's answer is completely correct. You could also add the IsVisibleChanged in the xaml code. For Example: in the .xaml

<Label x:Name="label1" Content="Label" IsVisibleChanged="Label_IsVisibleChanged" />

and in the xaml.cs

    private void Label_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
       //do stuff
    }
  • Related