wpf 如何在 xaml 中声明故事板并从代码中运行它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23632497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how declare a storyboard in xaml and run it from code
提问by saeid ezzati
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.
但我想在 xaml 中声明我的故事板并从代码中运行它。
but I dont know how do this ??
但我不知道这是怎么做的??
回答by Eli Arbel
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:
您可以将它放在资源字典中并从代码中引用它。或者,您可以使用事件触发器在 XAML 中启动 Storyboard:
<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:
从 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" />
回答by Patrick Hofman
- Declare the storyboard as resource in your Window.
Give it a key.
<Window.Resources> <Storyboard x:Key="test"> ... </Storyboard> </Window.Resources>Find the resource:
Storyboard sb = this.FindResource("test") as Storyboard;Use it:
sb.Begin();
- 在您的窗口中将故事板声明为资源。
给它一把钥匙。
<Window.Resources> <Storyboard x:Key="test"> ... </Storyboard> </Window.Resources>找到资源:
Storyboard sb = this.FindResource("test") as Storyboard;用它:
sb.Begin();
回答by Manuel Marini
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>

