Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Not directly in XAML, however you could reference a <code>DataTemplateSelector</code> from XAML to choose the correct template.</p> <pre><code>public class CustomerTemplateSelector : DataTemplateSelector { public override DataTemplate SelectTemplate(object item, DependencyObject container) { DataTemplate template = null; if (item != null) { FrameworkElement element = container as FrameworkElement; if (element != null) { string templateName = item is ObservableCollection&lt;MyCustomer&gt; ? "MyCustomerTemplate" : "YourCustomerTemplate"; template = element.FindResource(templateName) as DataTemplate; } } return template; } } public class MyCustomer { public string CustomerName { get; set; } } public class YourCustomer { public string CustomerName { get; set; } } </code></pre> <p>The resource dictionary:</p> <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication1" &gt; &lt;DataTemplate x:Key="MyCustomerTemplate"&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="Auto"/&gt; &lt;RowDefinition Height="150"/&gt; &lt;/Grid.RowDefinitions&gt; &lt;TextBlock Text="My Customer Template"/&gt; &lt;ListBox ItemsSource="{Binding}" DisplayMemberPath="CustomerName" Grid.Row="1"/&gt; &lt;/Grid&gt; &lt;/DataTemplate&gt; &lt;DataTemplate x:Key="YourCustomerTemplate"&gt; &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition Height="Auto"/&gt; &lt;RowDefinition Height="150"/&gt; &lt;/Grid.RowDefinitions&gt; &lt;TextBlock Text="Your Customer Template"/&gt; &lt;ListBox ItemsSource="{Binding}" DisplayMemberPath="CustomerName" Grid.Row="1"/&gt; &lt;/Grid&gt; &lt;/DataTemplate&gt; &lt;/ResourceDictionary&gt; </code></pre> <p>The window XAML:</p> <pre><code>&lt;Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300" xmlns:local="clr-namespace:WpfApplication1" &gt; &lt;Grid&gt; &lt;Grid.Resources&gt; &lt;local:CustomerTemplateSelector x:Key="templateSelector"/&gt; &lt;/Grid.Resources&gt; &lt;ContentControl Content="{Binding}" ContentTemplateSelector="{StaticResource templateSelector}" /&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>The window code behind:</p> <pre><code>public partial class Window1 { public Window1() { InitializeComponent(); ObservableCollection&lt;MyCustomer&gt; myCustomers = new ObservableCollection&lt;MyCustomer&gt;() { new MyCustomer(){CustomerName="Paul"}, new MyCustomer(){CustomerName="John"}, new MyCustomer(){CustomerName="Mary"} }; ObservableCollection&lt;YourCustomer&gt; yourCustomers = new ObservableCollection&lt;YourCustomer&gt;() { new YourCustomer(){CustomerName="Peter"}, new YourCustomer(){CustomerName="Chris"}, new YourCustomer(){CustomerName="Jan"} }; //DataContext = myCustomers; DataContext = yourCustomers; } } </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