4

I want to increase current window height when click on button.

I use this code:

private void sendbtn_Click(object sender, RoutedEventArgs e) { DoubleAnimation myDoubleAnimation = new DoubleAnimation(); myDoubleAnimation.From = this.Height; myDoubleAnimation.To = 500; myDoubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(0.5)); Storyboard myStoryboard = new Storyboard(); myStoryboard.Children.Add(myDoubleAnimation); Storyboard.SetTargetName(myDoubleAnimation, this.Name); Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Window.HeightProperty)); myStoryboard.Begin(this); } 

but I want declare my storyboard in xaml and run it from code.

but I dont know how do this ??

3 Answers 3

11

You can put it in a resource dictionary and reference it from code. Alternatively, you could use an event trigger to start the Storyboard in XAML:

<UserControl.Resources> <Storyboard x:Key="TheStoryboard"> <DoubleAnimation Storyboard.TargetProperty="Height" To="500" Duration="0:0:0.5" Storyboard.TargetName="X" /> <!-- no need to specify From --> </Storyboard> </UserControl.Resources> 

To start from code:

((Storyboard)this.Resources["TheStoryboard"]).Begin(this); 

To start from XAML:

<UserControl.Triggers> <EventTrigger RoutedEvent="ButtonBase.Click" SourceName="TheButton"> <BeginStoryboard Storyboard="{StaticResource TheStoryboard}"/> </EventTrigger> </UserControl.Triggers> 

Where the button is assigned the name:

 <Button Name="TheButton" Content="Start" /> 
Sign up to request clarification or add additional context in comments.

4 Comments

I added <UserControl.Resources> to <window>. but I have this error: The attached property 'UserControl.Resources' is not defined on 'Window' or one of its base classes. exactly where must I add <UserControl.Resources> ??
ok. now I have an exception that say: Additional information: 'X' name cannot be found in the name scope of 'firedl.MainWindow'.. now I must change Storyboard.TargetName="X" to what ??
@saeid "X" is just a placeholder he put there to represent the name of whatever object you're applying the animation to, so change "X" to the name of your object.
now I want to change value of To="500" from code section. how ??
5
  1. Declare the storyboard as resource in your Window.
  2. Give it a key.

    <Window.Resources> <Storyboard x:Key="test"> ... </Storyboard> </Window.Resources> 
  3. Find the resource:

    Storyboard sb = this.FindResource("test") as Storyboard; 
  4. Use it:

    sb.Begin(); 

Comments

0
Storyboard sb = (Storyboard)btnPause.FindResource("PauseStoryboard"); //to start sb.Begin(btnPause, true); //to stop sb.Stop(btnPause); <Button x:Name="btnPause" Width="28" Background="LightCyan" Click="btnPause_Click"> <Image Source="Images\pause.png"></Image> <Button.Resources> <Storyboard x:Key="PauseStoryboard"> <ColorAnimation To="Yellow" Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" Duration="0:0:1" RepeatBehavior="Forever" AutoReverse="True"/> </Storyboard> </Button.Resources> </Button> 

1 Comment

Please consider editing your post to add more explanation about what your code does and why it will solve the problem. An answer that mostly just contains code (even if it's working) usually wont help the OP to understand their problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.