Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 242297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:49:32+00:00 2026-05-11T20:49:32+00:00

How would I make a control fade in/out when it becomes Visible. Below is

  • 0

How would I make a control fade in/out when it becomes Visible.

Below is my failed attempt:

<Window x:Class="WadFileTester.Form1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Name="MyWindow" Title="WAD File SI Checker" Height="386" Width="563" WindowStyle="SingleBorderWindow" DragEnter="Window_DragEnter" DragLeave="Window_DragLeave" DragOver="Window_DragOver" Drop="Window_Drop" AllowDrop="True">
    <Window.Resources>
        <Style TargetType="ListView" x:Key="animatedList">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Visibility}" Value="Visible">
                    <DataTrigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation
                                    Storyboard.TargetProperty="Opacity"
                                    From="0.0" To="1.0" Duration="0:0:5"
                                    />
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                    <DataTrigger.ExitActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation
                                    Storyboard.TargetProperty="Opacity"
                                    From="1.0" To="0.0" Duration="0:0:5"
                                    />
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.ExitActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <ListView Name="listView1" Style="{StaticResource animatedList}" TabIndex="1" Margin="12,41,12,12" Visibility="Hidden">
        </ListView>
    </Grid>
</Window>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:49:33+00:00Added an answer on May 11, 2026 at 8:49 pm

    I don’t know how to do both animations (fade in and fade out) in pure XAML. But simple fade out can be achieved relatively simple. Replace DataTriggers with Triggers, and remove ExitActions since they makes no sense in Fade out scenario. This is what you will have:

     <Style TargetType="FrameworkElement" x:Key="animatedList">
      <Setter Property="Visibility" Value="Hidden"/>
      <Style.Triggers>
        <Trigger Property="Visibility" Value="Visible">
          <Trigger.EnterActions>
            <BeginStoryboard>
              <Storyboard>
                <DoubleAnimation Storyboard.TargetProperty="Opacity"
                                 From="0.0" To="1.0" Duration="0:0:0.2"/>
              </Storyboard>
            </BeginStoryboard>
          </Trigger.EnterActions>
        </Trigger>
      </Style.Triggers>
    </Style>
    

    But hey, don’t give up. If you want to support both animations I can suggest small coding behind the XAML. After we do a trick, we will get what you want by adding one line of code in XAML:

    <Button Content="Fading button"
            x:Name="btn"
            loc:VisibilityAnimation.IsActive="True"/>
    

    Every time we change btn.Visibility from Visible to Hidden/Collapsed button will fade out. And every time we change Visibility back the button will fade in. This trick will work with any FrameworkElement (including ListView 🙂 ).

    Here is the code of VisibilityAnimation.IsActive attached property:

      public class VisibilityAnimation : DependencyObject
      {
        private const int DURATION_MS = 200;
    
        private static readonly Hashtable _hookedElements = new Hashtable();
    
        public static readonly DependencyProperty IsActiveProperty =
          DependencyProperty.RegisterAttached("IsActive", 
          typeof(bool), 
          typeof(VisibilityAnimation),
          new FrameworkPropertyMetadata(false, new PropertyChangedCallback(OnIsActivePropertyChanged)));
    
        public static bool GetIsActive(UIElement element)
        {
          if (element == null)
          {
            throw new ArgumentNullException("element");
          }
    
          return (bool)element.GetValue(IsActiveProperty);
        }
    
        public static void SetIsActive(UIElement element, bool value)
        {
          if (element == null)
          {
            throw new ArgumentNullException("element");
          }
          element.SetValue(IsActiveProperty, value);
        }
    
        static VisibilityAnimation()
        {
          UIElement.VisibilityProperty.AddOwner(typeof(FrameworkElement),
                                                new FrameworkPropertyMetadata(Visibility.Visible, new PropertyChangedCallback(VisibilityChanged), CoerceVisibility));
        }
    
        private static void VisibilityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
          // So what? Ignore.
        }
    
        private static void OnIsActivePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
          var fe = d as FrameworkElement;
          if (fe == null)
          {
            return;
          }
          if (GetIsActive(fe))
          {
            HookVisibilityChanges(fe);
          }
          else
          {
            UnHookVisibilityChanges(fe);
          }
        }
    
        private static void UnHookVisibilityChanges(FrameworkElement fe)
        {
          if (_hookedElements.Contains(fe))
          {
            _hookedElements.Remove(fe);
          } 
        }
    
        private static void HookVisibilityChanges(FrameworkElement fe)
        {
          _hookedElements.Add(fe, false);
        }
    
        private static object CoerceVisibility(DependencyObject d, object baseValue)
        {
          var fe = d as FrameworkElement;
          if (fe == null)
          {
            return baseValue;
          }
    
          if (CheckAndUpdateAnimationStartedFlag(fe))
          {
            return baseValue;
          }
          // If we get here, it means we have to start fade in or fade out
          // animation. In any case return value of this method will be
          // Visibility.Visible. 
    
          var visibility = (Visibility)baseValue;
    
          var da = new DoubleAnimation
          {
            Duration = new Duration(TimeSpan.FromMilliseconds(DURATION_MS))
          };
    
          da.Completed += (o, e) =>
                            {
                              // This will trigger value coercion again
                              // but CheckAndUpdateAnimationStartedFlag() function will reture true
                              // this time, and animation will not be triggered.
                              fe.Visibility = visibility;
                              // NB: Small problem here. This may and probably will brake 
                              // binding to visibility property.
                            };
    
          if (visibility == Visibility.Collapsed || visibility == Visibility.Hidden)
          {
            da.From = 1.0;
            da.To = 0.0;
          }
          else
          {
            da.From = 0.0;
            da.To = 1.0;
          }
    
          fe.BeginAnimation(UIElement.OpacityProperty, da);
          return Visibility.Visible;
        }
    
        private static bool CheckAndUpdateAnimationStartedFlag(FrameworkElement fe)
        {
          var hookedElement = _hookedElements.Contains(fe);
          if (!hookedElement)
          {
            return true; // don't need to animate unhooked elements.
          }
    
          var animationStarted = (bool) _hookedElements[fe];
          _hookedElements[fe] = !animationStarted;
    
          return animationStarted;
        }
      }
    

    The most important thing here is CoerceVisibility() method. As you can see we do not allow changing this property until fading animation is completed.

    This code is neither thread safe nor bug free. Its only intention is to show the direction :). So feel free to improve, edit and get reputation ;).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 156k
  • Answers 156k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I suppose you could use some regex to reduce the… May 12, 2026 at 10:51 am
  • Editorial Team
    Editorial Team added an answer The main types of memory in any high-level language are… May 12, 2026 at 10:51 am
  • Editorial Team
    Editorial Team added an answer For determining if the polygon is convex, you could use… May 12, 2026 at 10:51 am

Related Questions

I'm new to WPF. I have like 15 grids on my Window and I
Using Zend form how would I make a control (element) an array? An example
I tend to use a StatusStrip at the bottom of most of my applications
I have a legacy VB6 ActiveX control used in IE to provide control of

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.