Click here to Skip to main content
15,897,891 members
Home / Discussions / C#
   

C#

 
QuestionProgrammatically Animating a StackPanel using C# in WPF Pin
Member 1322493123-Jun-17 5:20
Member 1322493123-Jun-17 5:20 
I'm writing a C# WPF application that contains two stacked StackPanels. When the user clicks on Panel1 it's height value changes expanding the space between Panel1 and Panel2. When the user clicks on Panel2 it's height is expanded and Panel1's height is set back to it's original value. I am able to get this working in the xaml code:

<pre>        <StackPanel Margin="1,1,1,1" Grid.ColumnSpan="3">
            <StackPanel Name="Panel1" Height="26">
                <Label HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Content="Menu_1" Foreground="White" FontWeight="Bold" MouseLeftButtonUp="Panel1_MouseLeftButtonUp">
                    <Label.Background>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FF80CEF3" Offset="0.01"/>
                            <GradientStop Color="#FF5588A0" Offset="1"/>
                        </LinearGradientBrush>
                    </Label.Background>
                </Label>
                <StackPanel.Triggers>
                    <EventTrigger RoutedEvent="UIElement.MouseLeftButtonUp">
                        <BeginStoryboard >
                            <Storyboard x:Name="Animation1">
                                <DoubleAnimation Storyboard.TargetName="Panel1" Storyboard.TargetProperty="Height" To="646" Duration="0:0:0.50" AccelerationRatio="0.6" DecelerationRatio="0.4" />
                                <DoubleAnimation Storyboard.TargetName="Panel2" Storyboard.TargetProperty="Height" To="26" Duration="0:0:0.50" AccelerationRatio="0.6" DecelerationRatio="0.4" />
                             </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </StackPanel.Triggers>
            </StackPanel>
            <StackPanel Name="Panel2" Height="26">
                <Label HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Content="Menu_2" Foreground="White" FontWeight="Bold">
                    <Label.Background>
                        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                            <GradientStop Color="#FF80CEF3" Offset="0.01"/>
                            <GradientStop Color="#FF5588A0" Offset="1"/>
                        </LinearGradientBrush>
                    </Label.Background>
                </Label>
                <StackPanel.Triggers>
                    <EventTrigger RoutedEvent="UIElement.MouseLeftButtonUp">
                        <BeginStoryboard>
                            <Storyboard x:Name="menu">
                                <DoubleAnimation x:Name="Menu_1" Storyboard.TargetName="Panel1" Storyboard.TargetProperty="Height" To="26" Duration="0:0:0.50" AccelerationRatio="0.6" DecelerationRatio="0.4" />
                                <DoubleAnimation x:Name="Menu_2" Storyboard.TargetName="Panel2" Storyboard.TargetProperty="Height" To="646" Duration="0:0:0.50" AccelerationRatio="0.6" DecelerationRatio="0.4" />
                           </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </StackPanel.Triggers>
            </StackPanel>
        </StackPanel>


However, I'm trying to do the same thing programmatically, so that I can determine at runtime what the new height should be when the user clicks on a panel. I implemented the following EventHandler, but it doesn't work (the Panel heights do not change).
private void Panel1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
      {

         DoubleAnimation oMenuAnimation = new DoubleAnimation();

          oMenuAnimation.From = 26;
          oMenuAnimation.To =646;
          oMenuAnimation.Duration = new Duration(new TimeSpan(0, 0, 0, 500));
          oMenuAnimation.AccelerationRatio = .6;
          oMenuAnimation.DecelerationRatio = .4;
          Storyboard s = new Storyboard();
          Storyboard.SetTargetProperty(oMenuAnimation, new PropertyPath("Height"));
          Storyboard.SetTarget(oMenuAnimation,Panel1);
          Storyboard.SetTargetName(oMenuAnimation, "Panel1");
          s.Children.Add(oMenuAnimation);


          DoubleAnimation oMenuAnimation2 = new DoubleAnimation();


          oMenuAnimation2.To = 26;
          oMenuAnimation2.Duration = new Duration(new TimeSpan(0, 0, 0, 500));
          oMenuAnimation2.AccelerationRatio = .6;
          oMenuAnimation2.DecelerationRatio = .4;

          Storyboard.SetTargetProperty(oMenuAnimation2, new PropertyPath("Height"));
          Storyboard.SetTarget(oMenuAnimation2, Panel2);

          s.Children.Add(oMenuAnimation2);



          s.Begin();

      }


Does anyone know what I may be doing wrong?
QuestionCreate an automatisation for a toolbar and checkboxes on a third party application Pin
Member 1302589023-Jun-17 4:12
Member 1302589023-Jun-17 4:12 
QuestionC# cross compile Pin
Chris-197323-Jun-17 3:47
Chris-197323-Jun-17 3:47 
AnswerRe: C# cross compile Pin
Afzaal Ahmad Zeeshan23-Jun-17 4:28
professionalAfzaal Ahmad Zeeshan23-Jun-17 4:28 
AnswerRe: C# cross compile Pin
Eddy Vluggen23-Jun-17 7:08
professionalEddy Vluggen23-Jun-17 7:08 
QuestionVisual Studio extension - Watch window item selection Pin
Abyss22-Jun-17 9:28
Abyss22-Jun-17 9:28 
QuestionEnd the Scheduled Task if it is running for more than couple of hours C# code Pin
indian14322-Jun-17 8:33
indian14322-Jun-17 8:33 
AnswerRe: End the Scheduled Task if it is running for more than couple of hours C# code Pin
Eddy Vluggen22-Jun-17 8:49
professionalEddy Vluggen22-Jun-17 8:49 
GeneralRe: End the Scheduled Task if it is running for more than couple of hours C# code Pin
indian14322-Jun-17 9:13
indian14322-Jun-17 9:13 
GeneralRe: End the Scheduled Task if it is running for more than couple of hours C# code Pin
Eddy Vluggen22-Jun-17 9:44
professionalEddy Vluggen22-Jun-17 9:44 
Questionhow to remove Flag status from Outlook by c# programming or register edit Pin
Member 1323094822-Jun-17 2:38
Member 1323094822-Jun-17 2:38 
AnswerRe: how to remove Flag status from Outlook by c# programming or register edit Pin
Chris Quinn22-Jun-17 3:09
Chris Quinn22-Jun-17 3:09 
AnswerRe: how to remove Flag status from Outlook by c# programming or register edit Pin
Eddy Vluggen22-Jun-17 4:21
professionalEddy Vluggen22-Jun-17 4:21 
GeneralRe: how to remove Flag status from Outlook by c# programming or register edit Pin
Member 1323094822-Jun-17 20:21
Member 1323094822-Jun-17 20:21 
GeneralRe: how to remove Flag status from Outlook by c# programming or register edit Pin
Pete O'Hanlon22-Jun-17 21:23
mvePete O'Hanlon22-Jun-17 21:23 
Question[SOLVED] Unify Methods; Parameters one or two IEnums<T> Pin
atrus271119-Jun-17 22:02
atrus271119-Jun-17 22:02 
AnswerRe: Unify Methods; Parameters one or two IEnums<T> Pin
Richard Deeming20-Jun-17 0:56
mveRichard Deeming20-Jun-17 0:56 
GeneralRe: Unify Methods; Parameters one or two IEnums<T> Pin
atrus271120-Jun-17 2:19
atrus271120-Jun-17 2:19 
GeneralRe: Unify Methods; Parameters one or two IEnums<T> Pin
Richard Deeming20-Jun-17 2:25
mveRichard Deeming20-Jun-17 2:25 
GeneralRe: Unify Methods; Parameters one or two IEnums<T> Pin
atrus271120-Jun-17 2:39
atrus271120-Jun-17 2:39 
PraiseRe: Unify Methods; Parameters one or two IEnums<T> Pin
atrus271120-Jun-17 20:32
atrus271120-Jun-17 20:32 
Questionbehavior of Value Tuples in .NET 4.7 Pin
BillWoodruff19-Jun-17 6:24
professionalBillWoodruff19-Jun-17 6:24 
AnswerRe: behavior of Value Tuples in .NET 4.7 Pin
Nathan Minier19-Jun-17 7:05
professionalNathan Minier19-Jun-17 7:05 
GeneralRe: behavior of Value Tuples in .NET 4.7 Pin
BillWoodruff20-Jun-17 6:36
professionalBillWoodruff20-Jun-17 6:36 
GeneralRe: behavior of Value Tuples in .NET 4.7 Pin
Nathan Minier20-Jun-17 7:34
professionalNathan Minier20-Jun-17 7:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.