2011-08-23 187 views
5

帮我看看。我需要在z轴上旋转按钮的旋转而不使用外部库,只能使用C#和xaml代码。WPF旋转按钮

这可能吗?我怎样才能做到这一点?

谢谢。

回答

11

已经在链接一看Viewport2DVisual3D

的例子正是这么做的。

编辑:这里是从与Z的添加动画链接轴

的例子看起来这
enter image description here

<Viewport3D> 
    <Viewport3D.Camera> 
     <PerspectiveCamera Position="0, 0, 4"/> 
    </Viewport3D.Camera> 
    <Viewport2DVisual3D x:Name="v2dv3d"> 
     <Viewport2DVisual3D.Transform> 
      <RotateTransform3D> 
       <RotateTransform3D.Rotation> 
        <AxisAngleRotation3D Angle="0" Axis="0, 1, 0" /> 
       </RotateTransform3D.Rotation> 
      </RotateTransform3D> 
     </Viewport2DVisual3D.Transform> 
     <Viewport2DVisual3D.Geometry> 
      <MeshGeometry3D Positions="-1,1,0 -1,-1,0 1,-1,0 1,1,0" 
        TextureCoordinates="0,0 0,1 1,1 1,0" TriangleIndices="0 1 2 0 2 3"/> 
     </Viewport2DVisual3D.Geometry> 

     <Viewport2DVisual3D.Material> 
      <DiffuseMaterial Viewport2DVisual3D.IsVisualHostMaterial="True" Brush="White"/> 
     </Viewport2DVisual3D.Material> 
     <Button Content="Hello, 3D"> 
      <Button.Triggers> 
       <EventTrigger RoutedEvent="FrameworkElement.Loaded"> 
        <BeginStoryboard> 
         <Storyboard RepeatBehavior="Forever"> 
          <Rotation3DAnimation Storyboard.TargetName="v2dv3d" 
                Storyboard.TargetProperty="(Viewport2DVisual3D.Transform).(RotateTransform3D.Rotation)" 
                Duration="0:0:2" 
                BeginTime="0:0:0"> 
           <Rotation3DAnimation.From> 
            <AxisAngleRotation3D Angle="0" Axis="0, 1, 0" /> 
           </Rotation3DAnimation.From> 
           <Rotation3DAnimation.To> 
            <AxisAngleRotation3D Angle="90" Axis="0, 1, 0" /> 
           </Rotation3DAnimation.To> 
          </Rotation3DAnimation> 
          <Rotation3DAnimation Storyboard.TargetName="v2dv3d" 
                Storyboard.TargetProperty="(Viewport2DVisual3D.Transform).(RotateTransform3D.Rotation)" 
                Duration="0:0:2" 
                BeginTime="0:0:2"> 
           <Rotation3DAnimation.From> 
            <AxisAngleRotation3D Angle="-90" Axis="0, 1, 0" /> 
           </Rotation3DAnimation.From> 
           <Rotation3DAnimation.To> 
            <AxisAngleRotation3D Angle="0" Axis="0, 1, 0" /> 
           </Rotation3DAnimation.To> 
          </Rotation3DAnimation> 
         </Storyboard> 
        </BeginStoryboard> 
       </EventTrigger> 
      </Button.Triggers> 
     </Button> 
    </Viewport2DVisual3D> 
    <ModelVisual3D> 
     <ModelVisual3D.Content> 
      <DirectionalLight Color="#FFFFFFFF" Direction="0,0,-1"/> 
     </ModelVisual3D.Content> 
    </ModelVisual3D> 
</Viewport3D> 
+0

谢谢您的回答。我不确切。我需要动画这个过程。 – Yevgeniy

+0

@Eugene:用z轴动画更新了我的答案。粘贴并尝试:) –

+0

@Meleak这是一个Y轴旋转。如果将轴更改为Z,则还可以简化故事板,因为按钮不会从相机中转开。 – Kimberly

1

转换可以帮助你在这种情况下。如果有帮助,请看here

RotateTransform类用于旋转X-Y平面中的WPF对象。它可以通过XAML或通过命令式代码直接应用。

1

如果您只需要旋转关于Z轴的按钮,那么您将不需要任何3D图形。所有的UIElements(例如按钮)都有属性RenderTransform,可以对其默认外观进行基本转换。通过Storyboards,WPF允许你animate almost any dependency property。您可以使用一个故事板,触发负载,动画应用到按钮RotateTransform的Angle属性:

<Button Width="100" Height="100" Content="Wheeee!"> 
    <Button.Triggers> 
    <EventTrigger RoutedEvent="FrameworkElement.Loaded"> 
     <BeginStoryboard> 
     <Storyboard Storyboard.TargetName="ButtonRotation" Storyboard.TargetProperty="Angle"> 
      <DoubleAnimation From="0" To="360" Duration="0:0:3" RepeatBehavior="Forever"/> 
     </Storyboard> 
     </BeginStoryboard> 
    </EventTrigger> 
    </Button.Triggers> 
    <Button.RenderTransform> 
    <RotateTransform x:Name="ButtonRotation" CenterX="50" CenterY="50" Angle="45"/> 
    </Button.RenderTransform> 
</Button> 

的Viewport2DVisual3D的建议,@Meleak,还支持动画,是乐趣,如果玩你有时间。进行动画显示MSDN例如,需要一个名称添加到AxisAngleRotation3D元件并切换到目标Z轴:

<AxisAngleRotation3D x:Name="RotateAboutZ" Angle="40" Axis="0, 0, 1" /> 

然后,如上所述,触发一个情节串连图板来开始对的Viewport3D的Loaded事件。在任何一种情况下,如果您需要对动画进行更多控制,则可以使故事板成为其他事件引用的命名资源,甚至可以完全用代码构建和控制它。