简体   繁体   English

创建接受变量的情节提要模板

[英]Create Storyboard Template That Accepts Variables

I'm using a storyboard animation multiple times in my WPF application, and I would prefer to create one style I can edit rather than multiple entries. 我在WPF应用程序中多次使用情节提要动画,并且我希望创建一种可以编辑的样式,而不是多个条目。 However, i need to pass in a reference to two particular StackPanel each time. 但是,我每次都需要传递对两个特定StackPanel的引用。 Is there an XAML only solution to this, or is working with the code behind the only way? 是否有仅XAML的解决方案,或者仅以唯一的方式使用代码?

The variables in question would be the datePicker and classPicker StackPanels referenced below. 有问题的变量将是下面引用的datePicker和classPicker StackPanels。

<BeginStoryboard>
  <Storyboard>
    <DoubleAnimation Storyboard.TargetProperty="Opacity" 
                     Storyboard.TargetName="classPicker"
                     From="1.0" To="0" Duration="0:0:0.25"
                     BeginTime="0:0:0"/>
    <DoubleAnimation Storyboard.TargetProperty="Opacity" 
                     Storyboard.TargetName="datePicker"
                     From="0" To="1.0" Duration="0:0:0.25"
                     BeginTime="0:0:0.25"/>
    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="classPicker"                                                                                    
                                   Storyboard.TargetProperty="Visibility">
      <DiscreteObjectKeyFrame KeyTime="0:0:0.25" 
                              Value="{x:Static Visibility.Collapsed}"/>
    </ObjectAnimationUsingKeyFrames>
    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="datePicker"                                                                                
                                   Storyboard.TargetProperty="Visibility">
      <DiscreteObjectKeyFrame KeyTime="0:0:0.25" 
                              Value="{x:Static Visibility.Visible}"/>
    </ObjectAnimationUsingKeyFrames>
  </Storyboard>
</BeginStoryboard>

To make it truly generic one should create a custom control which contains the controls you want to swap out with the story board operation. 为了使其真正通用,应该创建一个自定义控件,其中包含要与情节提要操作交换出来的控件。

Once the control is created one can drop in 1-* of those controls on the page and make them visible and execute the storyboard(s) as needed. 创建控件后,您可以在页面中放入1- *这些控件,并使它们可见并根据需要执行情节提要。

Following @OmegaMan's suggestion, I created a custom control which inherited from the Button class. 按照@OmegaMan的建议,我创建了一个自Button类继承的自定义控件。 Using DependencyProperties, I was able to pass along the elements from the XAML that I wanted the animation to affect. 使用DependencyProperties,我可以传递我希望动画影响的XAML元素。

Custom Class: 自订类别:

public partial class ButtonWithAnim : Button
{
    public Grid GridToFadeOut
    {
        get { return (Grid)GetValue(GridToFadeOutProperty); }
        set { SetValue(GridToFadeOutProperty, value); }
    }

    // Using a DependencyProperty as the backing store for GridToFadeOut.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty GridToFadeOutProperty =
        DependencyProperty.Register("GridToFadeOut", typeof(Grid), typeof(ButtonWithAnim));

    public Grid GridToFadeIn
    {
        get { return (Grid)GetValue(GridToFadeInProperty); }
        set { SetValue(GridToFadeInProperty, value); }
    }

    // Using a DependencyProperty as the backing store for GridToFadeIn.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty GridToFadeInProperty =
        DependencyProperty.Register("GridToFadeIn", typeof(Grid), typeof(ButtonWithAnim));

    protected override void OnClick()
    {
        ButtonClickHandler();
    }

    public void ButtonClickHandler()
    {
        DoubleAnimation fadeOutAnimation = new DoubleAnimation(1.0, 0.0, new TimeSpan(0, 0, 0, 0, 250));
        DoubleAnimation fadeInAnimation  = new DoubleAnimation(0.0, 1.0, new TimeSpan(0, 0, 0, 0, 250));

        fadeOutAnimation.Completed += (sender, eArgs) =>
        {
            GridToFadeOut.Visibility = Visibility.Collapsed;
            GridToFadeIn.Visibility = Visibility.Visible;
            GridToFadeIn.BeginAnimation(Grid.OpacityProperty, fadeInAnimation);
        };

        GridToFadeOut.BeginAnimation(Grid.OpacityProperty, fadeOutAnimation);
    }
}

XAML XAML

<custom:ButtonWithAnim Content="Test Button" Margin="0,10,25,0"
                               Grid.Row="1" Width="75" 
                               HorizontalAlignment="Right" 
                               GridToFadeOut="{Binding RelativeSource={RelativeSource AncestorType=Grid}}"
                               GridToFadeIn="{Binding ElementName=datePicker}" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM