I want to apply fade animation every time my window is shown. How to do that from xaml? That window can be hidden and then shown again so I can't use  Loaded event.
            Asked
            
        
        
            Active
            
        
            Viewed 5.1k times
        
    33
            
            
         
    
    
        Poma
        
- 8,174
- 18
- 82
- 144
- 
                    Look at this question, may be this will help: http://stackoverflow.com/questions/867656/fading-out-a-wpf-window-on-close – Seekeer Feb 08 '12 at 10:29
2 Answers
58
            
            
        You can use the ContentRendered event or override OnContentRendered virtual method like this:
    bool _shown;
    protected override void OnContentRendered(EventArgs e)
    {
        base.OnContentRendered(e);
        if (_shown)
            return;
        _shown = true;
        // Your code here.
    }
 
    
    
        ezolotko
        
- 1,723
- 1
- 21
- 21
- 
                    10Is it really needed to use _shown variable? Can rendered be called twice or more? – Jul 26 '17 at 16:01
- 
                    Yes, when your form shown twice or more. Mostly when form is child and needed to show and close again and again with 1 instance. – Gray Programmerz Mar 26 '21 at 13:41
5
            
            
        You could use the
IsVisibleChanged
Event from the WPF Window;
Then in the EventMethod use:
if((bool)e.IsVisible)
{
   // It became visible
}
else
{
  // It became hidden
}
This works with opening a new Window instance, this.Show(), this.hide(), this.Close()
 
    
    
        MiiChiel
        
- 201
- 1
- 4
- 14