C# 将动态对象应用于情节提要

C# 将动态对象应用于情节提要,c#,wpf,xaml,storyboard,C#,Wpf,Xaml,Storyboard,我在xaml文件中设置了以下storyboard <Window.Resources> <Storyboard x:Key="Storyboard1" > <DoubleAnimationUsingPath Duration="0:0:3" Source="X" Storyboard.TargetProperty="(UIElement.RenderTrans

我在
xaml
文件中设置了以下
storyboard

<Window.Resources>

    <Storyboard x:Key="Storyboard1" >
        <DoubleAnimationUsingPath 
            Duration="0:0:3"  
            Source="X" 

            Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)" 
            >
            <DoubleAnimationUsingPath.PathGeometry>
                <PathGeometry Figures="M1.5,-18.667014 L8.1669813,-54.00002 L31.833186,-94.666719 L60.166999,-116.33299"/>
            </DoubleAnimationUsingPath.PathGeometry>

        </DoubleAnimationUsingPath>
        <DoubleAnimationUsingPath 
            Duration="0:0:3"   
            Source="Y"

            Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" 
            >

            <DoubleAnimationUsingPath.PathGeometry>
                <PathGeometry Figures="M1.5,-18.667014 L8.1669813,-54.00002 L31.833186,-94.666719 L60.166999,-116.33299"/>
            </DoubleAnimationUsingPath.PathGeometry>

        </DoubleAnimationUsingPath>
    </Storyboard>
</Window.Resources>
在代码中我这样做

 var sub1 = FindResource("Storyboard1") as Storyboard;
 sub1.Begin(ObjectToMove);
到目前为止一切正常

问题

我不希望在
xaml
中静态设置
ObjectToMove
,而是希望能够有更多的动态设置,并使用
UserControl
从代码中应用它。因此,我设置了一个
UserControl

而不是这个
sub1.Begin(ObjectToMove)

是我干的

sub1.Begin(新托盘())

我一按下动画按钮,我就明白了

“[Unknown]”属性未指向路径“(0)。(1)[3](2)”中的DependencyObject

我做错了什么?任何帮助都将不胜感激

谢谢

更新#1-基于评论

我有这样的
托盘.xaml
。不过不确定

<UserControl x:Class="Test.Tray"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

         >
<Grid x:Name="LayoutRoot" Height="58" Width="50">
    <!--<Rectangle Fill="#FFD4CEC8" HorizontalAlignment="Left" Height="58" Stroke="White" VerticalAlignment="Top" Width="50"/>-->

    <Rectangle x:Name="ObjectToMove" Height="31" Canvas.Left="-316.5" Stroke="Black" Canvas.Top="431.5" Width="42" Fill="Aqua" RenderTransformOrigin="0.5,0.5" Margin="0,0,8,27">

        <Rectangle.RenderTransform>
            <TransformGroup>
                <ScaleTransform/>
                <SkewTransform/>
                <RotateTransform/>
                <TranslateTransform/>
            </TransformGroup>
        </Rectangle.RenderTransform>
    </Rectangle>

</Grid>

更新#2-基于给定答案

目标是设置多个故事板(比如7个故事板),但必须能够将对象动态应用于这些故事板。所以我必须控制每个故事板,并且能够在代码中为这些故事板提供这样做的对象

示例

第一次启动动画时,一个对象(称为对象#1)在路径上移动。当对象#1在其动画中只有几秒钟时,我必须能够将另一个对象(让我们称此对象为#2)推送/馈送到同一故事板。现在我们在故事板上看到了两个对象,它们在路径上一个接一个地移动。就像传送带一样。这两个对象之间有时间间隔(我会处理这个问题,不用担心)

注意:我将在故事板中使用
completed
方法,该方法在第一个故事板完成后立即触发。这样做,我应该有序列相似的动画和加上对故事板和对象的控制


希望上面的例子有点道理。

对不起,伙计,最近忙得不可开交……但我们得赶快把你分类,因为今天是星期五,我们俩在不久的将来都应该喝啤酒。:)

所以。。。你想让你的故事板是通用的,这样你就不必一直以特定的命名对象为目标,对吗?那么,让我们打开整个
TargetType
,接受我们的出价,让这些小家伙表现出来

使用上一个示例中的示例,我将重复使用相同的路径数据等。请注意,我们从唯一的对象获取了所有显式渲染声明,并将其隐式化。这样,它就不在乎它击中了哪个
矩形
,如果它是一个
矩形
,那么它就会被移动,因为我们已经将它全部移动到了一个不可知的样式中,控制
TargetType
,而不是像这样的
TargetName

<Canvas x:Name="Main" Height="1025" Width="1025">
  <Canvas.Resources>

    <Style TargetType="{x:Type Rectangle}">
      <Style.Resources>
        <!-- All your base are belong to us. -->
        <Storyboard x:Key="Weeeee" RepeatBehavior="Forever">
          <DoubleAnimationUsingPath Duration="0:0:3" Source="X" 
                                    Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
            <DoubleAnimationUsingPath.PathGeometry>
              <PathGeometry Figures="M0.83299852,-4.019 L0.83299852,0.6089829 L0.61794496,0.94214186 C-7.6701996,14.489389 -12.443,30.392629 -12.443001,47.403001 C-12.443,96.887715 27.948303,137.003 77.773499,137.003 C113.58536,137.003 144.52365,116.27938 159.09367,86.248303 L159.90265,84.471135 L380.931,84.471135 L380.931,86.871121 L160.63918,86.871121 L160.4838,87.217053 C145.62575,118.25356 114.07582,139.671 77.556,139.671 C26.745804,139.671 -14.444,98.212666 -14.444,47.071218 C-14.444,29.491346 -9.5768454,13.055669 -1.124851,-0.94513857 z"/>
            </DoubleAnimationUsingPath.PathGeometry>
          </DoubleAnimationUsingPath>
          <DoubleAnimationUsingPath Duration="0:0:3" Source="Y" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)">
            <DoubleAnimationUsingPath.PathGeometry>
              <PathGeometry Figures="M0.83299852,-4.019 L0.83299852,0.6089829 L0.61794496,0.94214186 C-7.6701996,14.489389 -12.443,30.392629 -12.443001,47.403001 C-12.443,96.887715 27.948303,137.003 77.773499,137.003 C113.58536,137.003 144.52365,116.27938 159.09367,86.248303 L159.90265,84.471135 L380.931,84.471135 L380.931,86.871121 L160.63918,86.871121 L160.4838,87.217053 C145.62575,118.25356 114.07582,139.671 77.556,139.671 C26.745804,139.671 -14.444,98.212666 -14.444,47.071218 C-14.444,29.491346 -9.5768454,13.055669 -1.124851,-0.94513857 z"/>
            </DoubleAnimationUsingPath.PathGeometry>
          </DoubleAnimationUsingPath>
        </Storyboard>
      </Style.Resources>

      <!-- Because we don't really care what the parent is or where it lives anyway do we? -->
      <Style.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
          <BeginStoryboard Storyboard="{StaticResource Weeeee}"/>
        </EventTrigger>
      </Style.Triggers>

      <!-- Whaaaa? We can do this?? Hell yes we can... -->
      <Setter Property="RenderTransformOrigin" Value="0.5,0.5"/>
      <Setter Property="Rectangle.RenderTransform">
        <Setter.Value>
          <TransformGroup>
            <ScaleTransform/>
            <SkewTransform/>
            <RotateTransform/>
            <TranslateTransform/>
          </TransformGroup>
        </Setter.Value>
      </Setter>

   </Style>

  </Canvas.Resources>

  <Canvas.RenderTransform>
    <MatrixTransform Matrix="1.25 0 0 -1.25 -197.1231 961.58875"/>
  </Canvas.RenderTransform>

  <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path139746" Fill="#FFD3BC5F" Canvas.Left="399.4" Canvas.Top="-172.6">
    <Path.Data>
      <PathGeometry Figures="m 66.611178 552.33853 c -4.36996 0.6305 -8.8334 0.97293 -13.3752 0.97293 -18.11465 0 -35.027 -5.21161 -49.34949 -14.18974 l 21.64732 -36.67733 c 8.2963 5.25686 18.12667 8.31294 28.67512 8.31294 2.7121 0 5.37755 -0.20365 7.98106 -0.59283 l 2.23539 -0.38916 c 4.72288 -0.91109 9.2226 -2.43913 13.41755 -4.49964 l 35.933612 77.65818 c -12.53504 5.94167 -26.095762 10.06571 -40.347392 12.0342 0 0 -2.27764 -21.55688 -4.54327 -42.98252 l -2.2747 0.35297 z" FillRule="nonzero"/>
    </Path.Data>
  </Path>
  <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Fill="#FFD3BC5F" Canvas.Left="401" Canvas.Top="-173" >
    <Path.Data>
      <PathGeometry x:Name="path139750" Figures="m -21.311892 515.98804 c -11.7053 -15.57594 -18.6547 -34.92004 -18.6622 -55.85694 l 35.53987 0 5.06525 -0.024 c 0.63507 11.4535 4.86922 21.94 11.58023 30.3706 l 1.44647 1.7423 c 2.94008 3.3894 6.29329 6.4077 9.98888 8.9736 l -43.53754 73.76488 c -13.18956 -8.30539 -24.88746 -18.76176 -34.60186 -30.87743 0 0 17.3968 -13.23944 34.61076 -26.3386 l -1.42986 -1.75428 z" FillRule="nonzero"/>
    </Path.Data>
  </Path>
  <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path139780" Fill="#FFD3BC5F" Canvas.Left="401.4" Canvas.Top="-167.6">
    <Path.Data>
      <PathGeometry Figures="m 214.75854 460.1016 94.92962 0 0 69.86866 -94.92962 0 0 -69.86866 z" FillRule="nonzero"/>
    </Path.Data>
  </Path>
  <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path139784" Fill="#FFD3BC5F" Canvas.Left="401.4" Canvas.Top="-167.6">
    <Path.Data>
      <PathGeometry Figures="m 211.64803 529.825 -36.87191 0 -62.32503 -69.72236 100.95576 0 0 69.74499 -1.75882 -0.0226 z" FillRule="nonzero"/>
    </Path.Data>
  </Path>
  <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  Fill="#FFD3BC5F" Canvas.Top="-173" Canvas.Left="401">
    <Path.Data>
      <PathGeometry x:Name="path139798" Figures="m -41.482092 460.131 -43.5377 0 c 0.6744 30.93022 11.4414 59.36853 29.1278 82.17137 l 33.3799 -25.40184 c -11.9001 -15.82789 -18.9641 -35.4887 -18.97 -56.76953" FillRule="nonzero"/>
    </Path.Data>
  </Path>
  <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path139802" Fill="#FFD3BC5F" Canvas.Left="399.4" Canvas.Top="-172.6">
    <Path.Data>
      <PathGeometry Figures="m 139.82292 494.54117 c -13.01467 -14.5593 -25.97803 -29.06131 -30.40076 -34.00892 0 0 -2.72123 28.49563 -29.566602 43.70507 l 17.54746 37.92328 c 19.158502 -10.35383 34.278952 -27.22704 42.419902 -47.61943" FillRule="nonzero"/>
    </Path.Data>
  </Path>
  <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path139806" Fill="#FFD3BC5F" Canvas.Left="399.4" Canvas.Top="-172.6">
    <Path.Data>
      <PathGeometry Figures="m 98.037008 543.53024 17.776782 38.41802 c 23.1211 -11.42479 42.62202 -29.06882 56.29587 -50.76297 l 0.18403 -0.31676 c 0 0 -15.53072 -17.37553 -31.34655 -35.06934 -8.34762 20.43762 -23.62341 37.33197 -42.910132 47.73105" FillRule="nonzero"/>
    </Path.Data>
  </Path>
  <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path139810" Fill="#FFD3BC5F" Canvas.Left="399.4" Canvas.Top="-172.6">
    <Path.Data>
      <PathGeometry Figures="m 53.235488 554.82033 c -18.39362 0 -35.56859 -5.28853 -50.11595 -14.3994 l -21.08756 35.73002 c 21.04842 12.78086 45.75185 20.14348 72.17809 20.14348 5.74697 0 11.40815 -0.36201 16.97265 -1.03931 l -4.37747 -41.4213 c -4.43321 0.63805 -8.96144 0.98651 -13.56976 0.98651" FillRule="nonzero"/>
    </Path.Data>
  </Path>

  <!-- Trajectory -->
  <Path Data="M16.276999,1 L16.276999,5.627983 16.061945,5.9611419 C7.7738004,19.508389 3.0009999,35.41163 3.000999,52.422002 3.0009999,101.90672 43.392303,142.022 93.217499,142.022 129.02936,142.022 159.96765,121.29839 174.53767,91.267304 L175.34665,89.490136 396.375,89.490136 396.375,91.890122 176.08318,91.890122 175.9278,92.236054 C161.06975,123.27256 129.51982,144.69 93,144.69 42.189804,144.69 1,103.23167 1,52.090218 1,34.510346 5.8671546,18.07467 14.319149,4.0738615 z" Fill="#7F000000" Height="145.69" Canvas.Left="360.056" Stretch="Fill" Stroke="Red" StrokeThickness="2" Canvas.Top="236.648" Width="397.375"/>

  <!-- Because I like to move it, move it ~~ Add as many as you like, however you like, from wherever you like. -->
  <Rectangle Fill="LightBlue" Height="36" Stroke="Black" Width="41" Canvas.Left="355" Canvas.Top="223.667"/>

</Canvas>
<UserControl.Resources>
   <Style TargetType="{x:Type Rectangle}" BasedOn="{StaticResource NiftyRectangleAnim}"/>
</UserControl.Resources>
对此,

<Path Name="path139746" Fill="#FFD3BC5F" Canvas.Left="399.4" Canvas.Top="-172.6" Data="m 66.611178 552.33853 ..... z"/>
现在您可以将它放在一个位置,假设您希望将它应用于用户控件上的所有矩形实例。那你就加上这样的东西,

<Canvas x:Name="Main" Height="1025" Width="1025">
  <Canvas.Resources>

    <Style TargetType="{x:Type Rectangle}">
      <Style.Resources>
        <!-- All your base are belong to us. -->
        <Storyboard x:Key="Weeeee" RepeatBehavior="Forever">
          <DoubleAnimationUsingPath Duration="0:0:3" Source="X" 
                                    Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.X)">
            <DoubleAnimationUsingPath.PathGeometry>
              <PathGeometry Figures="M0.83299852,-4.019 L0.83299852,0.6089829 L0.61794496,0.94214186 C-7.6701996,14.489389 -12.443,30.392629 -12.443001,47.403001 C-12.443,96.887715 27.948303,137.003 77.773499,137.003 C113.58536,137.003 144.52365,116.27938 159.09367,86.248303 L159.90265,84.471135 L380.931,84.471135 L380.931,86.871121 L160.63918,86.871121 L160.4838,87.217053 C145.62575,118.25356 114.07582,139.671 77.556,139.671 C26.745804,139.671 -14.444,98.212666 -14.444,47.071218 C-14.444,29.491346 -9.5768454,13.055669 -1.124851,-0.94513857 z"/>
            </DoubleAnimationUsingPath.PathGeometry>
          </DoubleAnimationUsingPath>
          <DoubleAnimationUsingPath Duration="0:0:3" Source="Y" Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)">
            <DoubleAnimationUsingPath.PathGeometry>
              <PathGeometry Figures="M0.83299852,-4.019 L0.83299852,0.6089829 L0.61794496,0.94214186 C-7.6701996,14.489389 -12.443,30.392629 -12.443001,47.403001 C-12.443,96.887715 27.948303,137.003 77.773499,137.003 C113.58536,137.003 144.52365,116.27938 159.09367,86.248303 L159.90265,84.471135 L380.931,84.471135 L380.931,86.871121 L160.63918,86.871121 L160.4838,87.217053 C145.62575,118.25356 114.07582,139.671 77.556,139.671 C26.745804,139.671 -14.444,98.212666 -14.444,47.071218 C-14.444,29.491346 -9.5768454,13.055669 -1.124851,-0.94513857 z"/>
            </DoubleAnimationUsingPath.PathGeometry>
          </DoubleAnimationUsingPath>
        </Storyboard>
      </Style.Resources>

      <!-- Because we don't really care what the parent is or where it lives anyway do we? -->
      <Style.Triggers>
        <EventTrigger RoutedEvent="FrameworkElement.Loaded">
          <BeginStoryboard Storyboard="{StaticResource Weeeee}"/>
        </EventTrigger>
      </Style.Triggers>

      <!-- Whaaaa? We can do this?? Hell yes we can... -->
      <Setter Property="RenderTransformOrigin" Value="0.5,0.5"/>
      <Setter Property="Rectangle.RenderTransform">
        <Setter.Value>
          <TransformGroup>
            <ScaleTransform/>
            <SkewTransform/>
            <RotateTransform/>
            <TranslateTransform/>
          </TransformGroup>
        </Setter.Value>
      </Setter>

   </Style>

  </Canvas.Resources>

  <Canvas.RenderTransform>
    <MatrixTransform Matrix="1.25 0 0 -1.25 -197.1231 961.58875"/>
  </Canvas.RenderTransform>

  <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path139746" Fill="#FFD3BC5F" Canvas.Left="399.4" Canvas.Top="-172.6">
    <Path.Data>
      <PathGeometry Figures="m 66.611178 552.33853 c -4.36996 0.6305 -8.8334 0.97293 -13.3752 0.97293 -18.11465 0 -35.027 -5.21161 -49.34949 -14.18974 l 21.64732 -36.67733 c 8.2963 5.25686 18.12667 8.31294 28.67512 8.31294 2.7121 0 5.37755 -0.20365 7.98106 -0.59283 l 2.23539 -0.38916 c 4.72288 -0.91109 9.2226 -2.43913 13.41755 -4.49964 l 35.933612 77.65818 c -12.53504 5.94167 -26.095762 10.06571 -40.347392 12.0342 0 0 -2.27764 -21.55688 -4.54327 -42.98252 l -2.2747 0.35297 z" FillRule="nonzero"/>
    </Path.Data>
  </Path>
  <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Fill="#FFD3BC5F" Canvas.Left="401" Canvas.Top="-173" >
    <Path.Data>
      <PathGeometry x:Name="path139750" Figures="m -21.311892 515.98804 c -11.7053 -15.57594 -18.6547 -34.92004 -18.6622 -55.85694 l 35.53987 0 5.06525 -0.024 c 0.63507 11.4535 4.86922 21.94 11.58023 30.3706 l 1.44647 1.7423 c 2.94008 3.3894 6.29329 6.4077 9.98888 8.9736 l -43.53754 73.76488 c -13.18956 -8.30539 -24.88746 -18.76176 -34.60186 -30.87743 0 0 17.3968 -13.23944 34.61076 -26.3386 l -1.42986 -1.75428 z" FillRule="nonzero"/>
    </Path.Data>
  </Path>
  <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path139780" Fill="#FFD3BC5F" Canvas.Left="401.4" Canvas.Top="-167.6">
    <Path.Data>
      <PathGeometry Figures="m 214.75854 460.1016 94.92962 0 0 69.86866 -94.92962 0 0 -69.86866 z" FillRule="nonzero"/>
    </Path.Data>
  </Path>
  <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path139784" Fill="#FFD3BC5F" Canvas.Left="401.4" Canvas.Top="-167.6">
    <Path.Data>
      <PathGeometry Figures="m 211.64803 529.825 -36.87191 0 -62.32503 -69.72236 100.95576 0 0 69.74499 -1.75882 -0.0226 z" FillRule="nonzero"/>
    </Path.Data>
  </Path>
  <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  Fill="#FFD3BC5F" Canvas.Top="-173" Canvas.Left="401">
    <Path.Data>
      <PathGeometry x:Name="path139798" Figures="m -41.482092 460.131 -43.5377 0 c 0.6744 30.93022 11.4414 59.36853 29.1278 82.17137 l 33.3799 -25.40184 c -11.9001 -15.82789 -18.9641 -35.4887 -18.97 -56.76953" FillRule="nonzero"/>
    </Path.Data>
  </Path>
  <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path139802" Fill="#FFD3BC5F" Canvas.Left="399.4" Canvas.Top="-172.6">
    <Path.Data>
      <PathGeometry Figures="m 139.82292 494.54117 c -13.01467 -14.5593 -25.97803 -29.06131 -30.40076 -34.00892 0 0 -2.72123 28.49563 -29.566602 43.70507 l 17.54746 37.92328 c 19.158502 -10.35383 34.278952 -27.22704 42.419902 -47.61943" FillRule="nonzero"/>
    </Path.Data>
  </Path>
  <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path139806" Fill="#FFD3BC5F" Canvas.Left="399.4" Canvas.Top="-172.6">
    <Path.Data>
      <PathGeometry Figures="m 98.037008 543.53024 17.776782 38.41802 c 23.1211 -11.42479 42.62202 -29.06882 56.29587 -50.76297 l 0.18403 -0.31676 c 0 0 -15.53072 -17.37553 -31.34655 -35.06934 -8.34762 20.43762 -23.62341 37.33197 -42.910132 47.73105" FillRule="nonzero"/>
    </Path.Data>
  </Path>
  <Path xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="path139810" Fill="#FFD3BC5F" Canvas.Left="399.4" Canvas.Top="-172.6">
    <Path.Data>
      <PathGeometry Figures="m 53.235488 554.82033 c -18.39362 0 -35.56859 -5.28853 -50.11595 -14.3994 l -21.08756 35.73002 c 21.04842 12.78086 45.75185 20.14348 72.17809 20.14348 5.74697 0 11.40815 -0.36201 16.97265 -1.03931 l -4.37747 -41.4213 c -4.43321 0.63805 -8.96144 0.98651 -13.56976 0.98651" FillRule="nonzero"/>
    </Path.Data>
  </Path>

  <!-- Trajectory -->
  <Path Data="M16.276999,1 L16.276999,5.627983 16.061945,5.9611419 C7.7738004,19.508389 3.0009999,35.41163 3.000999,52.422002 3.0009999,101.90672 43.392303,142.022 93.217499,142.022 129.02936,142.022 159.96765,121.29839 174.53767,91.267304 L175.34665,89.490136 396.375,89.490136 396.375,91.890122 176.08318,91.890122 175.9278,92.236054 C161.06975,123.27256 129.51982,144.69 93,144.69 42.189804,144.69 1,103.23167 1,52.090218 1,34.510346 5.8671546,18.07467 14.319149,4.0738615 z" Fill="#7F000000" Height="145.69" Canvas.Left="360.056" Stretch="Fill" Stroke="Red" StrokeThickness="2" Canvas.Top="236.648" Width="397.375"/>

  <!-- Because I like to move it, move it ~~ Add as many as you like, however you like, from wherever you like. -->
  <Rectangle Fill="LightBlue" Height="36" Stroke="Black" Width="41" Canvas.Left="355" Canvas.Top="223.667"/>

</Canvas>
<UserControl.Resources>
   <Style TargetType="{x:Type Rectangle}" BasedOn="{StaticResource NiftyRectangleAnim}"/>
</UserControl.Resources>

然后,该用户控件上可用的所有矩形都将继承该样式并设置动画

或者说,您只想使用故事板,然后将其从样式中拉出,并将其放在其他一些资源中,这些资源可以在层次结构中引用。就像(在伪代码中)你可以把这个比特扔到任何需要继承的地方

<Blah.Resources>
   <Storyboard x:Key="Weeeee"............./>
</Blah.Resources>

并应用故事板。开始你使用的方式。您仍然可以将样式的其余部分应用于矩形对象(不包括style.Triggers),这样它们都可以获得必要的转换声明,但故事板不一定要包含在样式模板中


希望有点道理,这是一个有点难以解释的概念,但不,你不一定要有故事板的风格。参考资料,我这样做是为了你可以复制/粘贴整个东西在一个窗口中,并看到它去。不过,您需要对使用资源/继承层次结构和其他东西进行这两种方式的操作有一定的经验。

由于故事板动画没有目标,因此它尝试在根对象上设置
TranslateTransform.Y
的动画,但没有
RenderTransform
属性集。为了在新托盘对象上运行情节提要,您需要对其进行如下编码:

sub1.Begin(new Tray().ObjectToMove);

这将在新托盘中的
ObjectToMove
对象上而不是托盘本身上运行storyboad。

您似乎必须确保尝试制作动画的所有对象都需要动画属性(谈论
renderTransferorMgroup
)。你必须在
托盘中添加一个
。@Sinatr我已经更新了问题。我不确定这是不是正确的方法。。?还有其他想法吗?谢谢!我认为他的总体目标是使行为应用于targettype,而不是特定的命名对象,除非我读错了。@ChrisW。我想,既然OP试图这样做
sub1.Begin(newtray())他同意保持故事板的原样。但话说回来,我可能看错了,错过了他的意图。呃,我没有任何使用托盘的经验,所以如果我只是在无意中理解了意图,我也不会感到惊讶。哦,好吧,我相信他会以某种方式让我们知道的。祝你周末愉快@埃文我也试了一下,但什么也没发生。。?有什么想法吗?谢谢谢谢你,克里斯。你的答案似乎很有希望,尽管我无法找到故事板,如果它在
风格的范围内。我尝试将这个'var sub1=FindResource(“故事板1”)作为故事板;`我已经更新了这篇文章,希望能提供更多信息
sub1.Begin(new Tray().ObjectToMove);