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

  • SEARCH
  • Home
  • 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 612991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:57:11+00:00 2026-05-13T17:57:11+00:00

I want to toggle the opacity of a control (Button, TextBox, Panel, etc) in

  • 0

I want to toggle the opacity of a control (Button, TextBox, Panel, etc) in my WPF project and wanted to check to see if I had done it correctly.

My question is: Is this the type of functionality that you’d normally write in XAML or would you use code similar to that below to achieve the fade in/fade out result?

internal static class AnimationExtensions
{
    internal enum TransitionSpeed
    {
        Instant = 0,
        Fast = 100,
        Normal = 200,
        Slow = 500
    }

    /// <summary>
    /// Toggles the opacity of a control.
    /// </summary>
    /// <param name="control">The control.</param>
    internal static void ToggleControlFade(this Control control)
    {
        control.ToggleControlFade(TransitionSpeed.Normal);
    }

    /// <summary>
    /// Toggles the opacity of a control.
    /// </summary>
    /// <param name="control">The control.</param>
    /// <param name="speed">The speed.</param>
    internal static void ToggleControlFade(this Control control, TransitionSpeed speed)
    {
        Storyboard storyboard = new Storyboard();
        TimeSpan duration = new TimeSpan(0, 0, 0, 0, (int)speed); //

        DoubleAnimation animation = new DoubleAnimation { From = 1.0, To = 0.0, Duration = new Duration(duration) };
        if (control.Opacity == 0.0)
        {
            animation = new DoubleAnimation { From = 0.0, To = 1.0, Duration = new Duration(duration) };
        }

        Storyboard.SetTargetName(animation, control.Name);
        Storyboard.SetTargetProperty(animation, new PropertyPath("Opacity", 0));
        storyboard.Children.Add(animation);

        storyboard.Begin(control);
    }
}

As you can probably tell I’m very, very new to WPF.

Thanks

  • 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-13T17:57:12+00:00Added an answer on May 13, 2026 at 5:57 pm

    I tend to find in places where I need to perform an action after an animation or where the animation is dependent on complex triggers that the code-behind is the best place, otherwise the XAML is a good place to put the animation. (I usually do this for things like transitions or simple ‘onclick’ sort of events.

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

Sidebar

Related Questions

I want to use jQuery to toggle the opacity of an element between 0.3
I want to toggle en-ability/disability of two form fields based upon the radio button
I want my toggle button's ON text to be large and OFF text to
$('.button').click(function() { $('#small-form').animate({ width: ['toggle', 'swing'], height: ['toggle', 'swing'], opacity: 'toggle' }, 5000, 'linear');
I am using jquery UI button with its icon. I want to toggle the
I want to toggle between two values in Python, that is, between 0 and
I want to toggle element that id's pg when click link. header: <title>Example</title> <script
I want to toggle the ul when I click on group1 or group2 HTML
I want to toggle a process's visibility at runtime , I have a Windows
I want to toggle a class based on some condition which I have to

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.