Note that there are some explanatory texts on larger screens.

plurals
  1. POContentTemplateSelector is only called one time showing always the same datatemplate
    primarykey
    data
    text
    <p>I have made a sample demo VS 2010 RC sample project, because in my production project I have the same error using MVVM.</p> <p>In my sample demo project I use only Code-behind without 3rd party dependencies so you can download the demo project here and run it for yourself: <a href="http://www.sendspace.com/file/mwx7wv" rel="noreferrer">http://www.sendspace.com/file/mwx7wv</a></p> <p>Now to the problem: When I click the girls/boys button it should switch the datatemplate, not?</p> <p>What do I wrong?</p> <p>OK I offer here a code snippet too:</p> <p><strong>Code-Behind MainWindow.cs</strong>:</p> <pre><code>namespace ContentTemplateSelectorDemo { /// &lt;summary&gt; /// Interaction logic for MainWindow.xaml /// &lt;/summary&gt; public partial class MainWindow : Window { Person person; public MainWindow() { InitializeComponent(); person = new Person(){ Gender = "xxx"}; person.IsBoy = true; ContentGrid.DataContext = person; } private void btnBoys_Click(object sender, RoutedEventArgs e) { person.IsBoy = true; person.IsGirl = false; this.ContentGrid.DataContext = person; } private void btnGirls_Click(object sender, RoutedEventArgs e) { person.IsGirl = true; person.IsBoy = false; this.ContentGrid.DataContext = person; } } } </code></pre> <p><strong>XAML MainWindow.xaml:</strong></p> <pre><code>&lt;Window x:Class="ContentTemplateSelectorDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:ContentTemplateSelectorDemo" Title="MainWindow" Height="350" Width="525"&gt; &lt;Window.Resources&gt; &lt;DataTemplate x:Key="girlsViewTemplate"&gt; &lt;local:UserControl1 /&gt; &lt;/DataTemplate&gt; &lt;DataTemplate x:Key="boysViewTemplate" &gt; &lt;local:UserControl2 /&gt; &lt;/DataTemplate&gt; &lt;local:PersonDataTemplateSelector x:Key="PersonSelector" /&gt; &lt;/Window.Resources&gt; &lt;Grid x:Name="ContentGrid" &gt; &lt;StackPanel&gt; &lt;Button Name="btnGirls" Click="btnGirls_Click"&gt;Switch Girls&lt;/Button&gt; &lt;Button Name="btnBoys" Click="btnBoys_Click"&gt;Switch Boys&lt;/Button&gt; &lt;ContentControl Content="{Binding}" ContentTemplateSelector="{StaticResource ResourceKey=PersonSelector}" /&gt; &lt;/StackPanel&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p><strong>DataTemplateSelector class:</strong></p> <pre><code>public class PersonDataTemplateSelector : DataTemplateSelector { public override DataTemplate SelectTemplate(object item,DependencyObject container) { if (item is Person) { Person person = item as Person; Window window = Application.Current.MainWindow; if (System.ComponentModel.DesignerProperties.GetIsInDesignMode( window)) return null; if (person.IsBoy) return window.FindResource("boysViewTemplate") as DataTemplate; if (person.IsGirl) return window.FindResource("girlsViewTemplate") as DataTemplate; } return null; } } </code></pre> <p>:)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    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