Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>@Rachel is right. Your <code>DataTemplateSelector</code> only gets invoked once at load pretty much and not for <code>INPC</code> changes.</p> <p>What you can do for what you require is use a <code>ListBoxItemStyle</code> with <code>DataTrigger</code> switching the <code>Template</code> used</p> <p>something like:</p> <pre><code>&lt;ControlTemplate x:Key="greenItemTemplate"&gt; &lt;Border BorderBrush="Green" BorderThickness="3"&gt; &lt;StackPanel HorizontalAlignment="Stretch"&gt; &lt;TextBlock Text="{Binding Path=GroupName}" /&gt; &lt;TextBlock Text="{Binding Path=myType}" /&gt; &lt;/StackPanel&gt; &lt;/Border&gt; &lt;/ControlTemplate&gt; &lt;ControlTemplate x:Key="redItemTemplate"&gt; &lt;Border BorderBrush="Red" BorderThickness="3"&gt; &lt;StackPanel HorizontalAlignment="Stretch"&gt; &lt;TextBlock Text="{Binding Path=GroupName}" /&gt; &lt;TextBlock Text="{Binding Path=myType}" /&gt; &lt;/StackPanel&gt; &lt;/Border&gt; &lt;/ControlTemplate&gt; &lt;ControlTemplate x:Key="yellowItemTemplate"&gt; &lt;Border BorderBrush="Yellow" BorderThickness="3"&gt; &lt;StackPanel HorizontalAlignment="Stretch"&gt; &lt;TextBlock Text="{Binding Path=GroupName}" /&gt; &lt;TextBlock Text="{Binding Path=myType}" /&gt; &lt;/StackPanel&gt; &lt;/Border&gt; &lt;/ControlTemplate&gt; &lt;Style x:Key="MyListBoxItemStyle" TargetType="{x:Type ListBoxItem}"&gt; &lt;Setter Property="Template" Value="{DynamicResource greenItemTemplate}" /&gt; &lt;Style.Triggers&gt; &lt;DataTrigger Binding="{Binding myType}" Value="c"&gt; &lt;Setter Property="Template" Value="{DynamicResource redItemTemplate}" /&gt; &lt;/DataTrigger&gt; &lt;DataTrigger Binding="{Binding myType}" Value="b"&gt; &lt;Setter Property="Template" Value="{DynamicResource yellowItemTemplate}" /&gt; &lt;/DataTrigger&gt; &lt;/Style.Triggers&gt; &lt;/Style&gt; </code></pre> <p>and usage:</p> <pre><code>&lt;ListBox ItemContainerStyle="{StaticResource MyListBoxItemStyle}" ItemsSource="{Binding myCollectionOfMyClassObjects}" Name="listBox1" HorizontalContentAlignment="Stretch" /&gt; </code></pre>
 

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