views:

42

answers:

1

Hi everyone! I have the following problem to solve: I have some ellipses in my xaml that work as buttons, and some of them may open in 2 new buttons when clicked. I put them in separate canvas, in a way that this buttons to be generated already exist with opacity 0. What I want is to have an effect to set this buttons opacity to 1 when I click their parent button, in a transition. How can I achieve that?

C#

        private void ExpandHarborButtons(object sender, MouseButtonEventArgs e)
        {
            Ellipse thisPath = (Ellipse)sender;
            String test = (String)thisPath.DataContext;
            for(int i = 0; i < DoubleHarbors.Children.Count; i++)
            {

               Ellipse button = (Ellipse)VisualTreeHelper.GetChild(DoubleHarbors, i);

               if (test.Contains((String)button.DataContext))
               {
                   button.Opacity = 1;
               }
            }
        }

That's the way I'm doing right now, but it doesn't work as I want. The buttons are shown, but not with the effect I told before.

+4  A: 

Create a DoubleAnimation and start it from the click. Something like this:

<Storyboard x:Name="fadeIn">
   <DoubleAnimation Storyboard.TargetName="ButtonName"  From="0.0" To="0.1" Duration="0:0:0.5"
                Soryboard.TargetProperty="Opacity"/>
</Storyboard>

Then in Code:

fadeIn.Begin();

--EDIT--

Here's how to do an animation in C#. It's actually easier to define in XAML, but if this is really what you want, this is a way to do it.

        Storyboard sb = new Storyboard();
        DoubleAnimation da = new DoubleAnimation();
        da.From = 0;
        da.To = 1.0;
        da.Duration = new Duration(new TimeSpan(0, 0, 0, 0, 500));

        sb.Children.Add(da);

        Storyboard.SetTarget(sb, sender as Button);
        Storyboard.SetTargetProperty(Button1, new PropertyPath("Opacity"));

        sb.Begin();
Robaticus
Can I create this in the cs file? In the way you showed, I would have to create a storyboard for which button?
Bruno
You can create the animation in the CS file. Not necessarily the way I would do it from an architecture standpoint, but technically, it works. I'll edit my answer to show.
Robaticus