title: WPF: Viewport3D (C#) inside a Canvas (XAML)
body: <p>I'm a beginner in WPF, so I don't understand exactly how C# works with XAML to make the application.</p>
  
  <p>I'm trying to make a software to draw and manipulate 3D geometry. I've followed <a href="https://docs.microsoft.com/en-us/dotnet/framework/wpf/graphics-multimedia/how-to-create-a-3-d-scene" rel="nofollow noreferrer">this example</a> on how to create a 3D scene and was succesful.</p>
  
  <p>But, when I try to put the <code>Viewport3D</code> inside a <code>canvas</code> in XAML (so that I can place other elements like buttons in the UI to manipulate the 3D scene), I can't figure out why the <code>Viewport3D</code> doesn't show up.</p>
  
  <p>I don't want to define everything of the <code>Viewport3D</code> in XAML because I plan to have more complex operations of drawing and manipulation of geometry. And I think it is better to do it in C#.</p>
  
  <p>The MainWindow from the C# file isn't the same on the XAML file?</p>
  
  <p>Here is the window I get:</p>
  
  <p><a href="https://i.stack.imgur.com/JOlsO.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JOlsO.png" alt="enter image description here"></a></p>
  
  <p>Here is the code:</p>
  
  <p>XAML</p>
  
  <pre><code>&lt;Window x:Name="Geology3D_MainWindow" x:Class="Geology3D.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
          xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
          xmlns:local="clr-namespace:Geology3D"
          mc:Ignorable="d"
          Title="Geology3D" Height="600" Width="800" Background="Silver"&gt;
      &lt;Canvas x:Name="Canvas3D" Background="White" Margin="10,29,16.6,7.4"&gt;
          &lt;Viewport3D x:Name="ViewPort3D"&gt;
  
          &lt;/Viewport3D&gt;
      &lt;/Canvas&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>C#</p>
  
  <pre><code>using System;
  using System.Diagnostics;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Data;
  using System.Windows.Documents;
  using System.Windows.Input;
  using System.Windows.Media;
  using System.Windows.Media.Media3D;
  using System.Windows.Media.Imaging;
  using System.Windows.Navigation;
  using System.Windows.Shapes;
  
  namespace Geology3D
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
  
              //Viewport3D ViewPort3D = new Viewport3D();
  
              Model3DGroup ModelsGroup = new Model3DGroup();
              ModelVisual3D GModelVisual3D = new ModelVisual3D();
  
              GeometryModel3D CubeGeometryModel = new GeometryModel3D();
              GeometryModel3D PlaneGeometryModel = new GeometryModel3D();
  
              PerspectiveCamera Camera = new PerspectiveCamera();
  
              // Specify where in the 3D scene the camera is.
              Camera.Position = new Point3D(-5, 5, -5);
  
              // Specify the direction that the camera is pointing.
              Camera.LookDirection = new Vector3D(1, -1, 1);
  
              // Asign the camera to the viewport
              ViewPort3D.Camera = Camera;
  
              // Define the lights cast in the scene. Without light, the 3D object cannot 
              // be seen. Note: to illuminate an object from additional directions, create 
              // additional lights.
              DirectionalLight GDirectionalLight = new DirectionalLight();
              GDirectionalLight.Color = Colors.White;
              GDirectionalLight.Direction = new Vector3D(-1, -1, -1);
  
              ModelsGroup.Children.Add(GDirectionalLight);
  
              // The material specifies the material applied to the 3D object.
              // Define material and apply to the mesh geometries.
              DiffuseMaterial BlueMaterial = new DiffuseMaterial(new SolidColorBrush(Colors.DodgerBlue));
              DiffuseMaterial PlaneMaterial = new DiffuseMaterial(new SolidColorBrush(Colors.Gray));
  
              CubeGeometryModel.Material = BlueMaterial;
              PlaneGeometryModel.Material = PlaneMaterial;
  
              // Apply the mesh to the geometry model.
              CubeGeometryModel.Geometry = DrawCube();
              PlaneGeometryModel.Geometry = DrawPlane();
  
              // Apply a transform to the object. In this sample, a rotation transform is applied,  
              // rendering the 3D object rotated.
              RotateTransform3D GRotateTransform3D = new RotateTransform3D();
              AxisAngleRotation3D GAxisAngleRotation3d = new AxisAngleRotation3D();
              GAxisAngleRotation3d.Axis = new Vector3D(0, 1, 0);
              GAxisAngleRotation3d.Angle = 0;
              GRotateTransform3D.Rotation = GAxisAngleRotation3d;
              CubeGeometryModel.Transform = GRotateTransform3D;
  
              // Add the geometry model to the model group.
              ModelsGroup.Children.Add(CubeGeometryModel);
              ModelsGroup.Children.Add(PlaneGeometryModel);
  
              // Add the group of models to the ModelVisual3d.
              GModelVisual3D.Content = ModelsGroup;
  
              ViewPort3D.Children.Add(GModelVisual3D);
          }
  
          MeshGeometry3D DrawCube()[...]
  
          MeshGeometry3D DrawPlane()[...]
  
      }
  }
  </code></pre>