C# 在多个控件中应用同一情节提要
我希望在代码隐藏中动态地将相同的故事板应用于其他按钮,如btn_b和btn_cC# 在多个控件中应用同一情节提要,c#,wpf,C#,Wpf,我希望在代码隐藏中动态地将相同的故事板应用于其他按钮,如btn_b和btn_c 如果我单击按钮b,它必须同时设置动画和按钮c。这可能不是最好的方法。也就是说,你可以这样做: public partial class MainWindow : Window { public MainWindow() { this.InitializeComponent(); // Insert code required on object creation be
如果我单击按钮b,它必须同时设置动画和按钮c。这可能不是最好的方法。也就是说,你可以这样做:
public partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
// Insert code required on object creation below this point.
}
private void btn_a_Click(object sender, RoutedEventArgs e)
{
Storyboard button_animation = (Storyboard)(FindResource("all_in_one"));
button_animation.Begin();
}
}
您需要从XAML中删除您的故事板.TargetName分配,才能使其生效
如果您需要一次启动多个故事板,只需在使用新的目标名称调用Storyboard.SetTargetName函数后再调用Begin()。是否有办法完全在XAML中完成此操作?我猜是用DataTrigger?
public partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
// Insert code required on object creation below this point.
}
private void btn_a_Click(object sender, RoutedEventArgs e)
{
Storyboard button_animation = (Storyboard)(FindResource("all_in_one"));
button_animation.Begin();
}
}
private void btnC_Click(object sender, RoutedEventArgs e)
{
Storyboard storyboard = Resources["all_in_one"] as Storyboard;
Storyboard.SetTargetName(storyboard, "btnC");
storyboard.Begin();
}