Welcome!

.NET Authors: Liz McMillan, Peter Silva, Yakov Werde, Matthew Pollicove , Kevin Benedict

Related Topics: .NET

.NET: Article

Dare to Use the Current Capabilities of Windows Presentation Foundation

And get 3D layout effects

But What Is a Panel?
Before thinking in a 3D panel it's important to review the main issues solved by the bi-dimensional panels.

Positioning
If we only have a Canvas to arrange controls in the bi-dimensional space, we will need to calculate and recalculate formulas using the Left and Top properties and the size of the controls. To avoid the repetition of some recalculation patterns, WPF includes some specialized panels for the bi-dimensional world: StackPanel, Grid, etc. A common goal of all these panels is the ability to position their contained elements in a bi-dimensional area.

Resizing
In addition to the above positioning functionality, according to its kind of distribution logic the panels should decide the final size to visualize each element contained inside of them.

Arrange and Measure
The type Panel is the main class in the WPF bi-dimensional layout system. Panel and its descendant types solve the positioning and resizing problems using the Arrange and Measure methods of each contained UIElement. Measure finds the desired size requested by each contained element and Arrange is called to place and resize them according to the desired size obtained from the property DesiredSize (which is calculated by each call to Measure).

Unfortunately till now UIElement3D didn't have analogous methods for the 3D world as can be seen in the note in Figure 4.

Unhappily it seems that the 3D world won't have these essential methods then. However, the good news is that at least there is a feature (Transformations) that could help to mitigate the problem of positioning 3D elements without recalculating their placements.

Tri-Dimensional Transformations
As in the bi-dimensional space there are linear-transformations for the tri-dimensional world. These transformations can be applied to any element of type Visual3D or Model3D, and to the Camera. The 3D elements can be rotated, scaled, and translated throughout the RotateTransform3D, ScaleTransform3D, and TranslateTransform3D transformations. There is also a special transformation called Transform3DGroup that can be used to group and apply more than one transformation over the same element. In Listing 1 we use these transformations to place the models according to the layout logic.

Definition of the Panel3D
Listing 1 shows an approach to implementing a Panel3D. The default value for the property Transform of a Visual3D is MatrixTransform3D.Identity; that's why the method Move in Listing 1 asks for this value to know if the Transform property isn't explicitly set yet. The function of this Move method is to support moving elements according to the parameter offset using the transformations TranslateTransform3D and Transformation3DGroup.

The method OnVisualChildrenChanged calls the corresponding method of the base class ModelVisual3D and invokes the Arrange method when a new element is added to the panel. This Arrange method is abstract and must be implemented by the concrete descendants of Panel3D as we will see in the following case of a CubePanel.

Definition of a CubePanel
A CubePanel will be a sort of tri-dimensional grid that can be defined as a descendant of the type Panel3D overriding the Arrange method, as shown in Listing 2. A Grid in WPF allows elements to be added in a plane area, aligning them in rows and columns. The goal of a CubePanel is to add a third dimension so that if you want to put an element in some row and column, you can use YIndex instead of Row, XIndex instead of Column, with the ability to send the element back or bring it to the front with very realistic depth perspective using a ZIndex attached property. These indices refer to one of the coordinate descriptions declared in the collections XCoord and YCoord (equivalent to ColumnDescriptions and RowDescriptions of the Grid type) and the newest ZCoord. Accordingly, the SizeX, SizeY, and SizeZ properties define the dimensions of the whole CubePanel (see Listing 2).

A Pawn on the Chess Table
Listing 3 illustrates an application example of this CubePanel. The XAML code is used to place five planes, one sphere, and one cone as shown in Figure 5. The types SphereBuilder, PlaneBuilder, and ConeBuilder are essentially used to build the mesh of these figures. Their property Geometry returns a MeshGeometry3D bound to the Geometry property of the GeometryModel3D to get the result in Figure 5. We could discuss these types further but won't for want of space.

Conclusion
The WPF team is working hard to increment the primitives and basic capabilities of the API 3D. This article invites readers to dare to use the current capabilities of WPF to obtain 3D layout effects while waiting for the next generation of WPF 3D. Enjoy it!

Resources

  • Natham, A. (2007). Windows Presentation Foundation Unleashed. SAMS Publications.
  • Griffiths, I., and Sells, C. (2005). Programming Windows Presentation Foundation. O'Reilly.
  • Petzold, C. (2006). Applications = Code + Markup: A Guide to the Microsoft Windows Presentation Foundation. Microsoft Press.

More about panels in WPF can be found in the following Spanish publications of the authors:

  • Del Valle, M.; Sierra, I.; Hernández, Y.; and Katrib, M. "Cómo definir nuestros propios paneles personalizados en WPF." DotNetmanía No 35. Marzo 2007.
  • Katrib, M.; Del Valle, M.; Sierra, I.; and Hernández, Y. "Windows Presentation Foundation." DotNetManía Cuaderno Técnico No 7. Netalia 2007.
  • Del Valle, M.; Sierra, I.;Yamil Hernández, Y.; and Katrib, M. "Entrando en la tercera dimensión." DotNetmanía No 37. Mayo 2007.

More Stories By Miguel Katrib

Miguel Katrib is a PhD and a professor in the Computer Science Department at the University of Havana. He is also the head of the WEBOO group dedicated to Web and object-oriented technologies. Miguel is also a scientific advisor in .NET for the software enterprise CARE Technologies, Denia, Spain.

More Stories By Mario del Valle

Mario del Valle is working toward his MS at the Computer Science Department at the University of Havana, and is a software developer at the WEBOO group dedicated to Web and object-oriented technologies.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.