One of the amazing things in WPF is how easy can be make 3D stuff, and even animate it. I’m gonna show my first experiment with 3D animations. The “CubeButtonStyle” Style was taken from the book “Windows Presentation Foundation Unleashed” (btw, If you want learn WPF you MUST have this book).
This is the code for our Window, no code behind needed this time
<Window x:Class=”BlogStuff.ButtonWindow”
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
Title=”Cube Button” Height=”600″ Width=”600″>
<Window.Resources>
<Style x:Key=”CubeButtonStyle” TargetType=”{x:Type Button}”>
<Setter Property=”Template”>
<Setter.Value>
<ControlTemplate>
<ControlTemplate.Triggers>
<!– When the button is pressed, spin the cube –>
<Trigger Property=”Button.IsPressed” Value=”true”>
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard TargetName=”RotateY” TargetProperty=”Angle”>
<DoubleAnimation Duration=”0:0:1″ From=”0″ To=”360″ DecelerationRatio=”1.0″/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
</ControlTemplate.Triggers>
<Viewport3D>
<Viewport3D.Camera>
<PerspectiveCamera Position=”2.9,2.65,2.9″ LookDirection=”-1,-1,-1″/>
</Viewport3D.Camera>
<Viewport3D.Children>
<ModelVisual3D x:Name=”Light”>
<ModelVisual3D.Content>
<DirectionalLight Direction=”-0.3,-0.4,-0.5″/>
</ModelVisual3D.Content>
</ModelVisual3D>
<ModelVisual3D x:Name=”Cube”>
<ModelVisual3D.Transform>
<RotateTransform3D>
<RotateTransform3D.Rotation>
<AxisAngleRotation3D x:Name=”RotateY” Axis=”0,1,0″ Angle=”0″/>
</RotateTransform3D.Rotation>
</RotateTransform3D>
</ModelVisual3D.Transform>
<ModelVisual3D.Content>
<GeometryModel3D>
<GeometryModel3D.Material>
<DiffuseMaterial>
<DiffuseMaterial.Brush>
<!– Use a VisualBrush to display the Button’s original
Background and Content on the faces of the cube. ViewboxUnits=”RelativeToBoundingBox”–>
<VisualBrush Stretch=”Fill” Transform=”1,0,0,-1,0,1″>
<VisualBrush.Visual>
<Label Content=”{Binding Path=Content,
RelativeSource={RelativeSource TemplatedParent}}”
Background=”{Binding Path=Background,
RelativeSource={RelativeSource TemplatedParent}}”
Foreground=”{Binding Path=Foreground,
RelativeSource={RelativeSource TemplatedParent}}”
FontSize=”9″
Margin=”5,5,5,5″
HorizontalContentAlignment=”Center”
VerticalAlignment=”Center”/>
</VisualBrush.Visual>
</VisualBrush>
</DiffuseMaterial.Brush>
</DiffuseMaterial>
</GeometryModel3D.Material>
<GeometryModel3D.Geometry>
<MeshGeometry3D
Positions=”1,1,-1 1,-1,-1 -1,-1,-1 -1,1,-1 1,1,1 -1,1,1 -1,-1,1
1,-1,1 1,1,-1 1,1,1 1,-1,1 1,-1,-1 1,-1,-1 1,-1,1 -1,-1,1 -1,-1,-1
-1,-1,-1 -1,-1,1 -1,1,1 -1,1,-1 1,1,1 1,1,-1 -1,1,-1 -1,1,1″
TriangleIndices=”0 1 2 0 2 3 4 5 6 4 6 7 8 9 10 8 10 11 12
13 14 12 14 15 16 17 18 16 18 19 20 21 22 20 22 23″
TextureCoordinates=”0,1 0,0 1,0 1,1 1,1 0,1 0,-0 1,0 1,1
0,1 0,-0 1,0 1,0 1,1 0,1 0,-0 0,0 1,-0 1,1 0,1 1,-0
1,1 0,1 0,0″/>
</GeometryModel3D.Geometry>
</GeometryModel3D>
</ModelVisual3D.Content>
</ModelVisual3D>
</Viewport3D.Children>
</Viewport3D>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height=”auto”/>
<RowDefinition Height=”*”/>
</Grid.RowDefinitions>
<Label Content=”Click on the button to spin the cube!” FontSize=”18″/>
<Button Style=”{StaticResource CubeButtonStyle}” Content=”Click Me”
Background=”LightGreen” Foreground=”Yellow” Grid.Row=”1″/>
</Grid>
</Window>
Get fun! ![]()

Duff said,
February 20, 2008 at 8:42 pm
Sup dude, 3 posts in 2 days? lol wpf factory or wtf…
Daniel Isaacs said,
March 4, 2008 at 3:00 am
Very cool demo; however, is it easier to write an application like this using code behind? I was just wondering because if you are starting in WPF it might be easier to get things accomplished using C# code than XAML, even though you will need some of the xaml tags regardless.
fernandojhoel said,
March 4, 2008 at 1:11 pm
Yes, you can do this with code behind, but if you do this you will never be able to take the best from the WPF new features. The most important part of this is that now you can divide the design and the logic. In this examples I’m using a ListView in the XAML and an ObservableCollection in the Code Behind, but thanks that I’m using DataBinding (one of the most powerful WPF’s features) a designer can use a ListBox or a Grid or a Canvas and style them however he wants WITHOUT touch the code behind.
And you if you are starting with WPF, write code behind because is “easier” is a BIG mistake because you will not be able to understand the advantages of XAML.
imduff said,
March 4, 2008 at 1:17 pm
I agree…
Doug said,
May 29, 2008 at 10:29 pm
How would you descrease the size of the cube?
Doug said,
May 29, 2008 at 10:30 pm
My Bad…width and height should do the trick
Dave said,
October 6, 2008 at 3:11 pm
In this example is it still possible to use the storyboard complete event?
jessie said,
May 13, 2009 at 2:34 pm
this demo isn’t working for me.
I tried everything but i keep getting a very annoying error while compiling
Error 1 The attribute ‘FieldModifier’ does not exist in XML namespace ‘http://schemas.microsoft.com/winfx/2006/xaml’. Line 70 Position 70. \Visual Studio 2008\Projects\webDemo\webDemo\Window1.xaml 70 70 webDemo
does anybody recognize this error and know how to fix this. been trying for the past hour and a half and cant figure it out…
loads of thx in advance
kind regards,
jessie