title: WPF: Canvas and zIndex? How does it work?
body: <p>I have the following layou:</p>
  
  <pre><code>&lt;s:SurfaceWindow x:Class="Prototype_Concept_2.SurfaceWindow1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:s="http://schemas.microsoft.com/surface/2008"
      Title="Prototype_Concept_2"
      &gt;
    &lt;s:SurfaceWindow.Resources&gt;
      &lt;ImageBrush x:Key="WindowBackground" Stretch="None" Opacity="0.6" ImageSource="pack://application:,,,/Resources/WindowBackground.jpg"/&gt;
    &lt;/s:SurfaceWindow.Resources&gt;
  
    &lt;Grid Background="{StaticResource WindowBackground}" &gt;
  
              &lt;Grid Name="ProjectsGrid"&gt;
                  &lt;Grid.RowDefinitions&gt;
                      &lt;RowDefinition Height="Auto"&gt;&lt;/RowDefinition&gt;
                      &lt;RowDefinition Height="*"&gt;&lt;/RowDefinition&gt;
                      &lt;RowDefinition Height="Auto"&gt;&lt;/RowDefinition&gt;
  
                  &lt;/Grid.RowDefinitions&gt;
                  &lt;Grid.ColumnDefinitions&gt;
                      &lt;ColumnDefinition Width="Auto"&gt;&lt;/ColumnDefinition&gt;
                      &lt;ColumnDefinition Width="*"&gt;&lt;/ColumnDefinition&gt;
                  &lt;/Grid.ColumnDefinitions&gt;
                  &lt;TextBox Name="ProjectsHeader" Grid.ColumnSpan="2" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" FontSize="25" Text="Please choose one of the following projects" Grid.Row="0"&gt;&lt;/TextBox&gt;
                  &lt;s:SurfaceButton Name="BottomButton" HorizontalAlignment="Right" FontSize="20" Width="100" Grid.Column="1" Grid.Row="2" Foreground="White" Content="Refresh"&gt;&lt;/s:SurfaceButton&gt;
                  &lt;s:SurfaceListBox Background="Black" Grid.ColumnSpan="2" Name="ProjectsList" Grid.Row="1" ItemsSource="{Binding Projects}"&gt;&lt;/s:SurfaceListBox&gt;
                  &lt;Label Name="ProjectsFooter" Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2" FontSize="15" Content="Fetching projects data ..."&gt;&lt;/Label&gt;
  
              &lt;/Grid&gt;
  
          &lt;Grid ShowGridLines="True"  Name="SmellHeader" Visibility="Collapsed"&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition Width="256"&gt;&lt;/ColumnDefinition&gt;
              &lt;ColumnDefinition Width="256"&gt;&lt;/ColumnDefinition&gt;
              &lt;ColumnDefinition Width="256"&gt;&lt;/ColumnDefinition&gt;
              &lt;ColumnDefinition Width="256"&gt;&lt;/ColumnDefinition&gt;
          &lt;/Grid.ColumnDefinitions&gt;
                  &lt;Grid.RowDefinitions&gt;
                      &lt;RowDefinition Height="38"&gt;&lt;/RowDefinition&gt;
                      &lt;RowDefinition Height="*"&gt;&lt;/RowDefinition&gt;
                  &lt;/Grid.RowDefinitions&gt;
  
              &lt;Rectangle Grid.Column="0" Grid.Row="0" Fill="Black"&gt;&lt;/Rectangle&gt;
              &lt;Viewbox Grid.Column="0" Grid.Row="0" s:Contacts.PreviewContactDown="BrainClass_PreviewContactDown"&gt;
                  &lt;Label Background="Black" Foreground="White" Content="BrainClass" HorizontalContentAlignment="Center"&gt;&lt;/Label&gt;
              &lt;/Viewbox&gt;
  
              &lt;Rectangle Grid.Column="1" Grid.Row="0" Fill="Black"&gt;&lt;/Rectangle&gt;
              &lt;Viewbox Grid.Column="1" Grid.Row="0"&gt;
                  &lt;Label Background="Black" Foreground="White" Content="God Class" HorizontalContentAlignment="Center"&gt;&lt;/Label&gt;
              &lt;/Viewbox&gt;
  
              &lt;Rectangle Grid.Column="2" Grid.Row="0" Fill="Black"&gt;&lt;/Rectangle&gt;
              &lt;Viewbox Grid.Column="2" Grid.Row="0"&gt;
                  &lt;Label Background="Black" Foreground="White" Content="Tradition Breaker" HorizontalContentAlignment="Center"&gt;&lt;/Label&gt;
              &lt;/Viewbox&gt;
  
              &lt;Rectangle Grid.Column="3" Grid.Row="0" Fill="Black"&gt;&lt;/Rectangle&gt;
              &lt;Viewbox Grid.Column="3" Grid.Row="0"&gt;
                  &lt;Label Background="Black" Foreground="White" Content="RefusedParent Bequest" HorizontalContentAlignment="Center"&gt;&lt;/Label&gt;
              &lt;/Viewbox&gt;
          &lt;/Grid&gt;
          &lt;Canvas Name="RootLayer" Grid.Row="1" Grid.ColumnSpan="4"&gt;
          &lt;/Canvas&gt;
      &lt;/Grid&gt;
  &lt;/s:SurfaceWindow&gt;
  </code></pre>
  
  <p>To the RootLayer I add some Ellipse. Later I want to reoder them:</p>
  
  <pre><code>internal void setFocus(SourceManager manager)
          {
              Console.WriteLine("Set focus to class " + getFullName());
  
              foreach (SourceFile sf in manager.getBrainClasses())
              {
                  sf.getVisualizer().Fill = Brushes.Red;
                  Canvas.SetZIndex(sf.getVisualizer(), 0);
              }
  
              this.getVisualizer().Fill = Brushes.Blue;
  
              Canvas.SetZIndex(this.getVisualizer(), 1);
  
              manager.window.RootLayer.InvalidateArrange();
              manager.window.RootLayer.InvalidateVisual();
          }
  </code></pre>
  
  <p>The Ellipse is referenced by this.getVisualizer();
  However, nothing changes? How can I bring one Ellipse to the front?</p>