+8  A: 

Don't manipulate the component tree. Instead, manipulate a model that represents the component tree. Then have your view bind to the various collections and properties in that model to produce its visuals.

What follows is a really simplified example. It just shows the concepts - please don't take it as indicative of how you should factor your code.

First, my model:

public abstract class Shape
{
    public double Left { get; set; }
    public double Top { get; set; }
}

public class Rectangle : Shape
{
    public double Width { get; set; }
    public double Height { get; set; }
}

Next, I expose a collection of said shapes (you would use another model to contain this collection). Then I bind to it in my view:

<Window x:Name="_root" x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
     <DataTemplate DataType="{x:Type local:Rectangle}">
      <Rectangle Width="{Binding Width}" Height="{Binding Height}" Stroke="Black"/>
     </DataTemplate>
    </Window.Resources>
    <ItemsControl DataContext="{Binding ElementName=_root}" ItemsSource="{Binding Shapes}">
     <ItemsControl.ItemsPanel>
      <ItemsPanelTemplate>
       <Canvas/>
      </ItemsPanelTemplate>
     </ItemsControl.ItemsPanel>
     <ItemsControl.ItemContainerStyle>
      <Style>
       <Setter Property="Canvas.Left" Value="{Binding Left}"/>
       <Setter Property="Canvas.Top" Value="{Binding Top}"/>
      </Style>
     </ItemsControl.ItemContainerStyle>
    </ItemsControl>
</Window>

HTH, Kent

Kent Boogaart
Jep. Binding should be your only friend.
Inferis
Thank you Kent. It is the answer I was looking for.
Przemek
MVVM is much easier in WPF than Silverlight. For example, DataTemplates in Silverlight do not support the DataType property.
Alan Le
+2  A: 

The fundamental problem in your question is confusing requirements of your users (manipulating objects that are represented by rectangles and ellipses (I'm only guessing)) with implementation details (appending Rectangles and Ellipses to Canvases).

Again, the different responsibilities in the MVVM pattern:

View

Translate the ViewModel into pixels and translate input events into method calls on the ViewModel.

This would be the actual Silverlight components (Rectangle, Ellipse, Canvas) binding against their DataContext and having a few very small event handlers or Commands or whatever.

Model

Hold data and business logic in a domain-specific way.

This represents the "mathematical" rectangles and ellipses your users are drawing.

ViewModel

Refine the Model in a UI-oriented and often use-case specific way.

Here you store the transient information like "currently selected object" that are relevant for a specific view but are not attributes of the underlying Model's concept.

Read my blog for more on my views on MVVM.

David Schmitt
@David Thanks for sharing and for your explanation.
Przemek