原文:WPF中反转3D列表项

WPF中反转3D列表项
                                                         周银辉

记得在苹果电脑中有一个很酷的3D旋转效果, 它可以将某项的正反面进行反转, 在WPF中可以很轻松地做到该效果.
点击这里查看也可以粘贴此代码XamlPad中查看:

WPF中反转3D列表项-LMLPHP<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
WPF中反转3D列表项-LMLPHP      xmlns:s="clr-namespace:System;assembly=mscorlib"
WPF中反转3D列表项-LMLPHP      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP  <Page.Resources>
WPF中反转3D列表项-LMLPHP    <x:Array Type="{x:Type s:String}" x:Key="src">
WPF中反转3D列表项-LMLPHP      <s:String>Foo</s:String>
WPF中反转3D列表项-LMLPHP      <s:String>Bar</s:String>
WPF中反转3D列表项-LMLPHP      <s:String>Spong</s:String>
WPF中反转3D列表项-LMLPHP      <s:String>One</s:String>
WPF中反转3D列表项-LMLPHP      <s:String>Two</s:String>
WPF中反转3D列表项-LMLPHP      <s:String>Three</s:String>
WPF中反转3D列表项-LMLPHP      <s:String>Four</s:String>
WPF中反转3D列表项-LMLPHP      <s:String>Five</s:String>
WPF中反转3D列表项-LMLPHP    </x:Array>
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP    <DataTemplate x:Key="frontTemplate">
WPF中反转3D列表项-LMLPHP      <GroupBox Header="Front" Background="White">
WPF中反转3D列表项-LMLPHP        <TextBlock FontSize="40" Foreground="Green" Text="{Binding}" />
WPF中反转3D列表项-LMLPHP      </GroupBox>
WPF中反转3D列表项-LMLPHP    </DataTemplate>
WPF中反转3D列表项-LMLPHP    <DataTemplate x:Key="backTemplate">
WPF中反转3D列表项-LMLPHP      <GroupBox Header="Back" Background="White">
WPF中反转3D列表项-LMLPHP        <StackPanel>
WPF中反转3D列表项-LMLPHP          <RadioButton Content="This" IsChecked="True" />
WPF中反转3D列表项-LMLPHP          <RadioButton Content="Is" />
WPF中反转3D列表项-LMLPHP          <RadioButton Content="The" />
WPF中反转3D列表项-LMLPHP          <RadioButton Content="Back" />
WPF中反转3D列表项-LMLPHP        </StackPanel>
WPF中反转3D列表项-LMLPHP      </GroupBox>
WPF中反转3D列表项-LMLPHP    </DataTemplate>
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP    <DataTemplate x:Key="flipItemTemplate">
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP      <!-- Note: Camera setup only works when this is square. -->
WPF中反转3D列表项-LMLPHP      <Grid Width="200" Height="200">
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP        <!-- Provides 3D rotation transition. Hidden except for when animation is
WPF中反转3D列表项-LMLPHP       active. -->
WPF中反转3D列表项-LMLPHP        <Viewport3D Grid.Column="0" x:Name="vp3D" Visibility="Hidden">
WPF中反转3D列表项-LMLPHP          <Viewport3D.Camera>
WPF中反转3D列表项-LMLPHP            <PerspectiveCamera x:Name="camera" Position="0,0,0.5" LookDirection="0,0,-1" FieldOfView="90" />
WPF中反转3D列表项-LMLPHP          </Viewport3D.Camera>
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP          <Viewport3D.Children>
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP            <ModelVisual3D>
WPF中反转3D列表项-LMLPHP              <ModelVisual3D.Content>
WPF中反转3D列表项-LMLPHP                <Model3DGroup>
WPF中反转3D列表项-LMLPHP                  <DirectionalLight Color="#444" Direction="0,0,-1" />
WPF中反转3D列表项-LMLPHP                  <AmbientLight Color="#BBB" />
WPF中反转3D列表项-LMLPHP                </Model3DGroup>
WPF中反转3D列表项-LMLPHP              </ModelVisual3D.Content>
WPF中反转3D列表项-LMLPHP            </ModelVisual3D>
WPF中反转3D列表项-LMLPHP            <ModelVisual3D>
WPF中反转3D列表项-LMLPHP              <ModelVisual3D.Content>
WPF中反转3D列表项-LMLPHP                <GeometryModel3D>
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP                  <!-- Simple flat, square surface -->
WPF中反转3D列表项-LMLPHP                  <GeometryModel3D.Geometry>
WPF中反转3D列表项-LMLPHP                    <MeshGeometry3D
WPF中反转3D列表项-LMLPHP                     TriangleIndices="0,1,2 2,3,0"
WPF中反转3D列表项-LMLPHP                     TextureCoordinates="0,1 1,1 1,0 0,0"
WPF中反转3D列表项-LMLPHP                     Positions="-0.5,-0.5,0 0.5,-0.5,0 0.5,0.5,0 -0.5,0.5,0" />
WPF中反转3D列表项-LMLPHP                  </GeometryModel3D.Geometry>
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP                  <!-- Front of shape shows the content of 'frontHost' -->
WPF中反转3D列表项-LMLPHP                  <GeometryModel3D.Material>
WPF中反转3D列表项-LMLPHP                    <DiffuseMaterial>
WPF中反转3D列表项-LMLPHP                      <DiffuseMaterial.Brush>
WPF中反转3D列表项-LMLPHP                        <VisualBrush Visual="{Binding ElementName=frontHost}" />
WPF中反转3D列表项-LMLPHP                      </DiffuseMaterial.Brush>
WPF中反转3D列表项-LMLPHP                    </DiffuseMaterial>
WPF中反转3D列表项-LMLPHP                  </GeometryModel3D.Material>
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP                  <!-- Back of shape shows the content of 'backHost' -->
WPF中反转3D列表项-LMLPHP                  <GeometryModel3D.BackMaterial>
WPF中反转3D列表项-LMLPHP                    <DiffuseMaterial>
WPF中反转3D列表项-LMLPHP                      <DiffuseMaterial.Brush>
WPF中反转3D列表项-LMLPHP                        <VisualBrush Visual="{Binding ElementName=backHost}">
WPF中反转3D列表项-LMLPHP                          <VisualBrush.RelativeTransform>
WPF中反转3D列表项-LMLPHP                            <!-- By default, this would come out backwards because we're on the
WPF中反转3D列表项-LMLPHP                                   back on the shape. Flip it to make it right. -->
WPF中反转3D列表项-LMLPHP                            <ScaleTransform ScaleX="-1" CenterX="0.5" />
WPF中反转3D列表项-LMLPHP                          </VisualBrush.RelativeTransform>
WPF中反转3D列表项-LMLPHP                        </VisualBrush>
WPF中反转3D列表项-LMLPHP                      </DiffuseMaterial.Brush>
WPF中反转3D列表项-LMLPHP                    </DiffuseMaterial>
WPF中反转3D列表项-LMLPHP                  </GeometryModel3D.BackMaterial>
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP                  <!-- Rotation transform used for transition. -->
WPF中反转3D列表项-LMLPHP                  <GeometryModel3D.Transform>
WPF中反转3D列表项-LMLPHP                    <RotateTransform3D>
WPF中反转3D列表项-LMLPHP                      <RotateTransform3D.Rotation>
WPF中反转3D列表项-LMLPHP                        <AxisAngleRotation3D x:Name="rotate" Axis="0,3,0" Angle="0" />
WPF中反转3D列表项-LMLPHP                      </RotateTransform3D.Rotation>
WPF中反转3D列表项-LMLPHP                    </RotateTransform3D>
WPF中反转3D列表项-LMLPHP                  </GeometryModel3D.Transform>
WPF中反转3D列表项-LMLPHP                </GeometryModel3D>
WPF中反转3D列表项-LMLPHP              </ModelVisual3D.Content>
WPF中反转3D列表项-LMLPHP            </ModelVisual3D>
WPF中反转3D列表项-LMLPHP          </Viewport3D.Children>
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP        </Viewport3D>
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP        <!-- We use a pair of nested Borders to wrap the content that's going to go on
WPF中反转3D列表项-LMLPHP       each side of the rotating model.
WPF中反转3D列表项-LMLPHP       The reason is that we need to be able to fade these real bits of UI in and out
WPF中反转3D列表项-LMLPHP       as we transition from front to back, but we need to make sure the VisualBrush
WPF中反转3D列表项-LMLPHP       in the 3D model doesn't also get faded out. So the VisualBrush uses the inner
WPF中反转3D列表项-LMLPHP       Border, while the fade is applied to the outer one.
WPF中反转3D列表项-LMLPHP  -->
WPF中反转3D列表项-LMLPHP        <Border x:Name="frontWrapper">
WPF中反转3D列表项-LMLPHP          <!-- Note, it's important that this element has visuals that completely fill the space, as
WPF中反转3D列表项-LMLPHP       otherwise it messes with the VisualBrush's size in the 3D model. Setting the background
WPF中反转3D列表项-LMLPHP       has that effect, even a transparent one. -->
WPF中反转3D列表项-LMLPHP          <Border x:Name="frontHost" Background="Transparent">
WPF中反转3D列表项-LMLPHP            <Border.Triggers>
WPF中反转3D列表项-LMLPHP              <EventTrigger RoutedEvent="Grid.MouseDown">
WPF中反转3D列表项-LMLPHP                <BeginStoryboard>
WPF中反转3D列表项-LMLPHP                  <Storyboard>
WPF中反转3D列表项-LMLPHP                    <!-- Make the Viewport3D visible only for the duration of the rotation. -->
WPF中反转3D列表项-LMLPHP                    <ObjectAnimationUsingKeyFrames
WPF中反转3D列表项-LMLPHP                           Storyboard.TargetName="vp3D"
WPF中反转3D列表项-LMLPHP                           Storyboard.TargetProperty="Visibility">
WPF中反转3D列表项-LMLPHP                      <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Static Visibility.Visible}" />
WPF中反转3D列表项-LMLPHP                      <DiscreteObjectKeyFrame KeyTime="0:0:1.1" Value="{x:Static Visibility.Hidden}" />
WPF中反转3D列表项-LMLPHP                    </ObjectAnimationUsingKeyFrames>
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP                    <!-- Make the background element visible. (It won't actually appear until it is
WPF中反转3D列表项-LMLPHP                 faded in right at the end of the animation.) -->
WPF中反转3D列表项-LMLPHP                    <ObjectAnimationUsingKeyFrames
WPF中反转3D列表项-LMLPHP                           Storyboard.TargetName="backWrapper"
WPF中反转3D列表项-LMLPHP                           Storyboard.TargetProperty="Visibility">
WPF中反转3D列表项-LMLPHP                      <DiscreteObjectKeyFrame KeyTime="0:0:1" Value="{x:Static Visibility.Visible}"/>
WPF中反转3D列表项-LMLPHP                    </ObjectAnimationUsingKeyFrames>
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP                    <!-- Hide the foreground element. It will already be invisible by this time
WPF中反转3D列表项-LMLPHP                 because we fade it out right at the start of the animation. However, until
WPF中反转3D列表项-LMLPHP                 we set its Visibility to Hidden, it will still be visible to the mouseWPF中反转3D列表项-LMLPHP -->
WPF中反转3D列表项-LMLPHP                    <ObjectAnimationUsingKeyFrames
WPF中反转3D列表项-LMLPHP                           Storyboard.TargetName="frontWrapper"
WPF中反转3D列表项-LMLPHP                           Storyboard.TargetProperty="Visibility">
WPF中反转3D列表项-LMLPHP                      <DiscreteObjectKeyFrame KeyTime="0:0:0.05" Value="{x:Static Visibility.Hidden}" />
WPF中反转3D列表项-LMLPHP                    </ObjectAnimationUsingKeyFrames>
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP                    <!-- Fade the front wrapper out. The Viewport3D is behind us, so it'll fade into
WPF中反转3D列表项-LMLPHP                 view at this point. The reason for fading is to avoid a visible step as we
WPF中反转3D列表项-LMLPHP                 switch from the real UI to the copy projected onto the 3D model. -->
WPF中反转3D列表项-LMLPHP                    <DoubleAnimation To="0" Duration="0:0:0.05"
WPF中反转3D列表项-LMLPHP                           Storyboard.TargetName="frontWrapper"
WPF中反转3D列表项-LMLPHP                           Storyboard.TargetProperty="Opacity" />
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP                    <!-- Fade the back wrapper in. Once the spin completes, we fade the real back UI
WPF中反转3D列表项-LMLPHP                 in over the Viewport3D - using a fade to avoid a sudden jolt between the
WPF中反转3D列表项-LMLPHP                 slightly fuzzy 3D look and the real UI. -->
WPF中反转3D列表项-LMLPHP                    <DoubleAnimation BeginTime="0:0:1.05" Duration="0:0:0.05" To="1"
WPF中反转3D列表项-LMLPHP                           Storyboard.TargetName="backWrapper"
WPF中反转3D列表项-LMLPHP                           Storyboard.TargetProperty="Opacity" />
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP                    <!-- 3D animation. Move the camera out slightly as we spin, so the model fits entirely
WPF中反转3D列表项-LMLPHP                 within the field of view. Rotate the model 180 degrees. -->
WPF中反转3D列表项-LMLPHP                    <Point3DAnimation To="0,0,1.1" From="0,0,0.5"
WPF中反转3D列表项-LMLPHP                           BeginTime="0:0:0.05" Duration="0:0:0.5" AutoReverse="True" DecelerationRatio="0.3"
WPF中反转3D列表项-LMLPHP                           Storyboard.TargetName="camera"
WPF中反转3D列表项-LMLPHP                           Storyboard.TargetProperty="(PerspectiveCamera.Position)" />
WPF中反转3D列表项-LMLPHP                    <DoubleAnimation From="0" To="180" AccelerationRatio="0.3" DecelerationRatio="0.3"
WPF中反转3D列表项-LMLPHP                           BeginTime="0:0:0.05" Duration="0:0:1"
WPF中反转3D列表项-LMLPHP                           Storyboard.TargetName="rotate"
WPF中反转3D列表项-LMLPHP                           Storyboard.TargetProperty="Angle" />
WPF中反转3D列表项-LMLPHP                  </Storyboard>
WPF中反转3D列表项-LMLPHP                </BeginStoryboard>
WPF中反转3D列表项-LMLPHP              </EventTrigger>
WPF中反转3D列表项-LMLPHP            </Border.Triggers>
WPF中反转3D列表项-LMLPHP            <ContentPresenter  Content="{Binding}" ContentTemplate="{StaticResource frontTemplate}" />
WPF中反转3D列表项-LMLPHP          </Border>
WPF中反转3D列表项-LMLPHP        </Border>
WPF中反转3D列表项-LMLPHP        <Border x:Name="backWrapper" Grid.Column="0"  Visibility="Hidden" Opacity="0">
WPF中反转3D列表项-LMLPHP          <Border x:Name="backHost" Background="Transparent">
WPF中反转3D列表项-LMLPHP            <Border.Triggers>
WPF中反转3D列表项-LMLPHP              <EventTrigger RoutedEvent="Grid.MouseDown">
WPF中反转3D列表项-LMLPHP                <BeginStoryboard>
WPF中反转3D列表项-LMLPHP                  <Storyboard>
WPF中反转3D列表项-LMLPHP                    <ObjectAnimationUsingKeyFrames
WPF中反转3D列表项-LMLPHP                           Storyboard.TargetName="vp3D"
WPF中反转3D列表项-LMLPHP                           Storyboard.TargetProperty="Visibility">
WPF中反转3D列表项-LMLPHP                      <DiscreteObjectKeyFrame KeyTime="0:0:0" Value="{x:Static Visibility.Visible}" />
WPF中反转3D列表项-LMLPHP                      <DiscreteObjectKeyFrame KeyTime="0:0:1.1" Value="{x:Static Visibility.Hidden}" />
WPF中反转3D列表项-LMLPHP                    </ObjectAnimationUsingKeyFrames>
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP                    <ObjectAnimationUsingKeyFrames
WPF中反转3D列表项-LMLPHP                           Storyboard.TargetName="frontWrapper"
WPF中反转3D列表项-LMLPHP                           Storyboard.TargetProperty="Visibility">
WPF中反转3D列表项-LMLPHP                      <DiscreteObjectKeyFrame KeyTime="0:0:1" Value="{x:Static Visibility.Visible}"/>
WPF中反转3D列表项-LMLPHP                    </ObjectAnimationUsingKeyFrames>
WPF中反转3D列表项-LMLPHP                    <ObjectAnimationUsingKeyFrames
WPF中反转3D列表项-LMLPHP                           Storyboard.TargetName="backWrapper"
WPF中反转3D列表项-LMLPHP                           Storyboard.TargetProperty="Visibility">
WPF中反转3D列表项-LMLPHP                      <DiscreteObjectKeyFrame KeyTime="0:0:0.05" Value="{x:Static Visibility.Hidden}" />
WPF中反转3D列表项-LMLPHP                    </ObjectAnimationUsingKeyFrames>
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP                    <DoubleAnimation To="0" Duration="0:0:0.05"
WPF中反转3D列表项-LMLPHP                           Storyboard.TargetName="backWrapper"
WPF中反转3D列表项-LMLPHP                           Storyboard.TargetProperty="Opacity" />
WPF中反转3D列表项-LMLPHP                    <DoubleAnimation BeginTime="0:0:1.05" Duration="0:0:0.05"
WPF中反转3D列表项-LMLPHP                           Storyboard.TargetName="frontWrapper"
WPF中反转3D列表项-LMLPHP                           Storyboard.TargetProperty="Opacity" />
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP                    <Point3DAnimation To="0,0,1.1" From="0,0,0.5"
WPF中反转3D列表项-LMLPHP                           BeginTime="0:0:0.05" Duration="0:0:0.5" AutoReverse="True" DecelerationRatio="0.3"
WPF中反转3D列表项-LMLPHP                           Storyboard.TargetName="camera"
WPF中反转3D列表项-LMLPHP                           Storyboard.TargetProperty="(PerspectiveCamera.Position)" />
WPF中反转3D列表项-LMLPHP                    <DoubleAnimation From="180" To="360" AccelerationRatio="0.3" DecelerationRatio="0.3"
WPF中反转3D列表项-LMLPHP                           BeginTime="0:0:0.05" Duration="0:0:1"
WPF中反转3D列表项-LMLPHP                           Storyboard.TargetName="rotate"
WPF中反转3D列表项-LMLPHP                           Storyboard.TargetProperty="Angle" />
WPF中反转3D列表项-LMLPHP                  </Storyboard>
WPF中反转3D列表项-LMLPHP                </BeginStoryboard>
WPF中反转3D列表项-LMLPHP              </EventTrigger>
WPF中反转3D列表项-LMLPHP            </Border.Triggers>
WPF中反转3D列表项-LMLPHP            <ContentPresenter Content="{Binding}" ContentTemplate="{StaticResource backTemplate}" />
WPF中反转3D列表项-LMLPHP          </Border>
WPF中反转3D列表项-LMLPHP        </Border>
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP      </Grid>
WPF中反转3D列表项-LMLPHP    </DataTemplate>
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP  </Page.Resources>
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP  <ScrollViewer>
WPF中反转3D列表项-LMLPHP    <ItemsControl ItemsSource="{StaticResource src}" ItemTemplate="{StaticResource flipItemTemplate}">
WPF中反转3D列表项-LMLPHP      <ItemsControl.ItemsPanel>
WPF中反转3D列表项-LMLPHP        <ItemsPanelTemplate>
WPF中反转3D列表项-LMLPHP          <WrapPanel />
WPF中反转3D列表项-LMLPHP        </ItemsPanelTemplate>
WPF中反转3D列表项-LMLPHP      </ItemsControl.ItemsPanel>
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP    </ItemsControl>
WPF中反转3D列表项-LMLPHP  </ScrollViewer>
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP</Page>
WPF中反转3D列表项-LMLPHP
WPF中反转3D列表项-LMLPHP

更多请参考这里: http://www.interact-sw.co.uk/iangblog/2007/05/17/wpf-flippable-3D-list

05-08 08:29