Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>ItemsControl</code> is designed for creating dynamic collections of UI controls from other collections, even non-UI data collections.</p> <p>You can template an <code>ItemsControl</code> to draw on a <code>Canvas</code>. The ideal way would involve setting the backing panel to a <code>Canvas</code> and then setting the <code>Canvas.Left</code> and <code>Canvas.Top</code> properties on the immediate children. I could not get this to work because <code>ItemsControl</code> wraps its children with containers and it is hard to set the <code>Canvas</code> properties on these containers. </p> <p>Instead, I use a <code>Grid</code> as a bin for all of the items and draw them each on their own <code>Canvas</code>. There is some overhead with this approach.</p> <pre><code>&lt;ItemsControl x:Name="Collection" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"&gt; &lt;ItemsControl.ItemsPanel&gt; &lt;ItemsPanelTemplate&gt; &lt;Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/&gt; &lt;/ItemsPanelTemplate&gt; &lt;/ItemsControl.ItemsPanel&gt; &lt;ItemsControl.ItemTemplate&gt; &lt;DataTemplate DataType="{x:Type local:MyPoint}"&gt; &lt;Canvas HorizontalAlignment="Stretch" VerticalAlignment="Stretch"&gt; &lt;Ellipse Width="10" Height="10" Fill="Black" Canvas.Left="{Binding X}" Canvas.Top="{Binding Y}"/&gt; &lt;/Canvas&gt; &lt;/DataTemplate&gt; &lt;/ItemsControl.ItemTemplate&gt; &lt;/ItemsControl&gt; </code></pre> <p>Here's the code behind that I used to set up the source collection:</p> <pre><code>List&lt;MyPoint&gt; points = new List&lt;MyPoint&gt;(); points.Add(new MyPoint(2, 100)); points.Add(new MyPoint(50, 20)); points.Add(new MyPoint(200, 200)); points.Add(new MyPoint(300, 370)); Collection.ItemsSource = points; </code></pre> <p><code>MyPoint</code> is a custom class that behaves just like the <code>System</code> version. I created it to demonstrate that you can use your own custom classes.</p> <p>One final detail: You can bind the ItemsSource property to any collection you want. For example: </p> <pre><code>&lt;ItemsControls ItemsSource="{Binding Document.Items}"&gt;&lt;!--etc, etc...--&gt; </code></pre> <p>For further details about ItemsControl and how it works, check out these documents: <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.aspx" rel="noreferrer">MSDN Library Reference</a>; <a href="http://msdn.microsoft.com/en-us/library/ms742521.aspx" rel="noreferrer">Data Templating</a>; <a href="http://www.drwpf.com/blog/Home/tabid/36/EntryID/13/Default.aspx" rel="noreferrer">Dr WPF's series on ItemsControl</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload