Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The layout of items in an ItemsControl is controlled via the ItemsControl.ItemsPanel property which is of type ItemsPanelTemplate. The default value for the ItemsControl.ItemsPanel property is indeed an instance of ItemsPanelTemplate that specifies a StackPanel but this is completely customizable.</p> <p>The code example (<a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol(VS.95).aspx" rel="nofollow noreferrer">on this MSDN page</a>) shown below the paragraph that starts "The following example creates an ItemsControl." is very useful in understanding what the ItemsControl.Template, ItemsControl.ItemsPanel and ItemsControl.ItemTemplate properties are for.</p> <p>There are a few ways to achieve what you describe in the second sentence of the first paragraph of your question. Here is a full example:</p> <p>Page.xaml:</p> <pre><code>&lt;UserControl x:Class="ItemsControlImages.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:app="clr-namespace:ItemsControlImages"&gt; &lt;UserControl.Resources&gt; &lt;DataTemplate x:Key="CountryTemplate"&gt; &lt;Canvas&gt; &lt;Image Canvas.Top="{Binding Location.Y}" Canvas.Left="{Binding Location.X}" Source="{Binding FlagImage}" /&gt; &lt;StackPanel Canvas.Top="{Binding Location.Y}" Canvas.Left="{Binding Location.X}"&gt; &lt;TextBlock Text="{Binding Title}" /&gt; &lt;TextBlock Text="{Binding Location}" /&gt; &lt;StackPanel.RenderTransform&gt; &lt;TranslateTransform Y="-32.0" /&gt; &lt;/StackPanel.RenderTransform&gt; &lt;/StackPanel&gt; &lt;/Canvas&gt; &lt;/DataTemplate&gt; &lt;/UserControl.Resources&gt; &lt;Canvas x:Name="LayoutRoot"&gt; &lt;Canvas.Background&gt; &lt;LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"&gt; &lt;GradientStop Color="#FFB2C6D5"/&gt; &lt;GradientStop Color="#FF1483D9" Offset="1"/&gt; &lt;/LinearGradientBrush&gt; &lt;/Canvas.Background&gt; &lt;ItemsControl ItemTemplate="{StaticResource CountryTemplate}"&gt; &lt;app:Country Title="Argentina" Location="56,218" FlagImage="Images/ARG.png" /&gt; &lt;app:Country Title="China" Location="368,66" FlagImage="Images/CHN.png" /&gt; &lt;app:Country Title="Ireland" Location="192,90" FlagImage="Images/IRE.png" /&gt; &lt;app:Country Title="New Zealand" Location="404,225" FlagImage="Images/NZ.png" /&gt; &lt;app:Country Title="United States" Location="40,80" FlagImage="Images/USA.png" /&gt; &lt;/ItemsControl&gt; &lt;/Canvas&gt; &lt;/UserControl&gt; </code></pre> <p>Country.cs:</p> <pre><code>using System.ComponentModel; using System.Windows; namespace ItemsControlImages { public class Country : INotifyPropertyChanged { private string title; private string flagImage; private Point location; public string Title { get { return this.title; } set { if ((object.ReferenceEquals(this.title, value) != true)) { this.title = value; this.RaisePropertyChanged("Title"); } } } public string FlagImage { get { return this.flagImage; } set { if ((object.ReferenceEquals(this.flagImage, value) != true)) { this.flagImage = value; this.RaisePropertyChanged("FlagImage"); } } } public Point Location { get { return this.location; } set { if ((this.location.Equals(value) != true)) { this.location = value; this.RaisePropertyChanged("Location"); } } } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; protected void RaisePropertyChanged(string propertyName) { PropertyChangedEventHandler propertyChanged = this.PropertyChanged; if (propertyChanged != null) { propertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } #endregion } } </code></pre> <p>That's all you need (along with the images in an Images folder) for this end result:</p> <p><a href="http://www.freeimagehosting.net/uploads/bec683b75e.png" rel="nofollow noreferrer">alt text http://www.freeimagehosting.net/uploads/bec683b75e.png</a></p> <p>Note even though the Images are in an ItemsControl they are positioned at the coordinates shown by binding the Left and Top attached properties of their parent Canvas to the value of the X and Y coordinates from the custom Location property.</p> <p>For more information on this sample and customizing the ItemsControl using templates in general you can check out <a href="http://petermcg.wordpress.com/customizing-the-silverlight-2-itemscontrol-with-templates/" rel="nofollow noreferrer">this blog post</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. 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.
 

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