Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can steal the ItemTemplateSelector concept from the WPF playbook. This is what that would look like:</p> <h2>App.xaml</h2> <pre><code>&lt;DataTemplate x:Key="MaleTemplate"&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Text="M - " /&gt; &lt;TextBlock Text="{Binding Name}" /&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;DataTemplate x:Key="FemaleTemplate"&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;TextBlock Text="F - " /&gt; &lt;TextBlock Text="{Binding Name}" /&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; </code></pre> <h2>MainPage.xaml</h2> <pre><code>&lt;UserControl.Resources&gt; &lt;local:PersonTemplateSelector x:Key="PersonTemplateSelector" /&gt; &lt;/UserControl.Resources&gt; &lt;Grid x:Name="LayoutRoot"&gt; &lt;local:CustomItemsControl ItemsSource="{Binding}" ItemTemplateSelector="{StaticResource PersonTemplateSelector}" /&gt; &lt;/Grid&gt; </code></pre> <h2>MainPage.xaml.cs</h2> <pre><code>public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); this.DataContext = Person.GetSampleData(); } } </code></pre> <h2>PersonTemplateSelector.cs</h2> <pre><code>public class PersonTemplateSelector : DataTemplateSelector { public override DataTemplate SelectTemplate(object item, DependencyObject container) { var person = item as Person; if (person != null) { switch (person.Gender) { case Gender.Male: return Application.Current.Resources["MaleTemplate"] as DataTemplate; case Gender.Female: return Application.Current.Resources["FemaleTemplate"] as DataTemplate; default: throw new ArgumentOutOfRangeException(); } } return null; } } </code></pre> <p>If this is something that makes since and you are interested in using you will need to use this CustomItemsControl, or some variation:</p> <h2>CustomItemsControl.cs</h2> <pre><code>public class CustomItemsControl : ItemsControl { public DataTemplateSelector ItemTemplateSelector { get { return (DataTemplateSelector)GetValue(ItemTemplateSelectorProperty); } set { SetValue(ItemTemplateSelectorProperty, value); } } public static readonly DependencyProperty ItemTemplateSelectorProperty = DependencyProperty.Register("ItemTemplateSelector", typeof(DataTemplateSelector), typeof(CustomItemsControl), new PropertyMetadata(null)); protected override void PrepareContainerForItemOverride(DependencyObject element, object item) { ContentPresenter presenter = element as ContentPresenter; if (presenter != null) { DataTemplate itemTemplate = null; if (base.ItemTemplate != null) { itemTemplate = base.ItemTemplate; } else if (this.ItemTemplateSelector != null) { itemTemplate = this.ItemTemplateSelector.SelectTemplate(item, element); } presenter.Content = item; presenter.ContentTemplate = itemTemplate; } } } public class DataTemplateSelector { public virtual DataTemplate SelectTemplate(object item, DependencyObject container) { return null; } } </code></pre>
    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.
 

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