title: How to create a custom MultiSelector / ItemsControl in WPF / C#
body: <p>I'm trying to create a diagramming application in C# / WPF. What I going for is somewhat similar to Microsoft Visio although I'm not trying to clone it. I kind of wrote this question as I was coding and just put all the issues I had into it in case someone will find it useful. Maybe I've been thinking too hard but I feel like I could throw up on my keyboard and produce better code, so feel free to give any suggestions on every detail you catch (grammar excluded :-))</p>
  
  <p><strong>In short:</strong><br>
  Why are all the items positioned at (0,0)?</p>
  
  <h2>Code:</h2>
  
  <pre><code>public class Diagram : MultiSelector
  {
      public Diagram()
      {
          this.CanSelectMultipleItems = true;
  
          // The canvas supports absolute positioning
          FrameworkElementFactory panel = new FrameworkElementFactory(typeof(Canvas)); 
          this.ItemsPanel = new ItemsPanelTemplate(panel);
  
  
          // Tells the container where to position the items
          this.ItemContainerStyle = new Style();
          this.ItemContainerStyle.Setters.Add(new Setter(Canvas.LeftProperty, new Binding("X")));
          this.ItemContainerStyle.Setters.Add(new Setter(Canvas.TopProperty, new Binding("Y")));
      }
  
      protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
      {
          FrameworkElement contentitem = element as FrameworkElement;
          Binding leftBinding = new Binding("X");
          Binding topBinding = new Binding("Y");
          contentitem.SetBinding(Canvas.LeftProperty, leftBinding);
          contentitem.SetBinding(Canvas.TopProperty, topBinding);
          base.PrepareContainerForItemOverride(element, item);
      }
  
      public class DiagramItem : ContentControl
      {
          private Point _location;
  
          public DiagramItem()
          {
          }
  
          static DiagramItem()
          {
  
          }
  
          public Point Location
          {
              get { return _location; }
              set
              {
                  _location = value;
  
              }
          }
  
          public double X
          {
              get { return _location.X; }
              set
              {
                  _location.X = value;
              }
          }
  
          public double Y
          {
              get { return _location.Y; }
              set
              {
                  _location.Y = value;
              }
          }
      }
  
  //...
  </code></pre>
  
  <p>Ok, so the idea is that the  <em>Diagram : ItemsControl</em> places its item on a Canvas panel at the position defined in the Item DiagramItem.Location. IOW when I change the X property in a DiagramItem the Diagram moves the item on the x-axis.</p>
  
  <p><strong>Note:</strong> MultiSelector is derived from ItemsControl and Selector and is only used here because I need the displayed item to be selectable.</p>
  
  <hr>
  
  <p>Please note that I'd prefer not to use xaml if possible.</p>
  
  <hr>
  
  <p><strong>In long:</strong><br>
  A Diagram instance as seen by the user has these requirements:</p>
  
  <ol>
  <li>Has multiple DiagramItems.</li>
  <li>User can select multiple DiagramItems.</li>
  <li>DiagramItems can be resized, rotated and dragged anywhere on the Diagram.</li>
  <li>Possible to navigate between DiagramItems using the keyboard.</li>
  </ol>
  
  <p>I basically have two and possibly three classes relevant to this question.</p>
  
  <ul>
  <li><strong>Diagram</strong> extends System.Windows.Controls.Primitives.<em>MultiSelector</em> : Selector : ItemsControl</li>
  <li><strong>DiagramItem</strong> extends <em>ContentControl</em> or some other Control</li>
  </ul>
  
  <p>The Diagram.ItemsPanel aka the visual panel which displays the items should be a panel which supports absolute positioning, like the <strong>Canvas</strong>.</p>
  
  <p>How should I implement a class derived from MultiSelector and what resources can you point at which are relevant to this question?</p>
  
  <p>What does one have to consider when implementing a custom MultiSelector / ItemsControl?</p>
  
  <hr>
  
  <p><strong>Resources:</strong></p>
  
  <p>I've found very few resources relevant to my issue, but then again I'm not sure what I'm supposed to be looking for. I've read the source code for ListBox and ListBoxItem using Reflector but didn't find it very useful.</p>
  
  <p><strong>Other resources:</strong> </p>
  
  <ul>
  <li><a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.multiselector.aspx" rel="nofollow noreferrer">System.Windows.Controls.Primitives.MultiSelector</a></li>
  <li><a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.aspx" rel="nofollow noreferrer">System.Windows.Controls.ItemsControl</a></li>
  <li><a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemspanel.aspx" rel="nofollow noreferrer">ItemsControl.ItemsPanel</a></li>
  <li><a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.canvas.aspx" rel="nofollow noreferrer">System.Windows.Controls.Canvas</a></li>
  <li><a href="http://forums.silverlight.net/forums/t/29753.aspx" rel="nofollow noreferrer">Positioning items when Canvas is the ItemsPanel of a ItemsControl</a></li>
  <li><a href="http://msdn.microsoft.com/en-us/magazine/cc163497.aspx" rel="nofollow noreferrer">Using Templates to customize WPF controls</a></li>
  <li><a href="http://msdn.microsoft.com/en-us/library/cc294882.aspx" rel="nofollow noreferrer">Create an items control</a></li>
  </ul>