Note that there are some explanatory texts on larger screens.

plurals
  1. POListbox ItemTemplate Selector does not pick a template
    primarykey
    data
    text
    <p>I am trying to use an ItemTemplateSelector on a listbox within a grid that I am creating on a different file to later be called by the MainWindow. </p> <p>Here is my DataTemplateSelector code</p> <pre><code> public class TemplateSelector : DataTemplateSelector { public override DataTemplate SelectTemplate(object item, DependencyObject container) { FrameworkElement element = container as FrameworkElement; if (element != null &amp;&amp; item != null &amp;&amp; item is myClass) { myClass agg = item as myClass; if(agg.myType == a) { return element.FindResource("greenItemTemplate") as DataTemplate; } else if (agg.myType == b) { return element.FindResource("yellowItemTemplate") as DataTemplate; } else if (agg.myType == c) { return element.FindResource("redItemTemplate") as DataTemplate; } } return null; } } </code></pre> <p>Here is my xaml</p> <pre><code> &lt;Grid x:Class="NS.Views.ListView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:c="clr-namespace:NS.Commons" DataContext="{Binding}"&gt; &lt;Grid.Resources&gt; &lt;c:myConverter x:Key="converter" /&gt; &lt;c:TemplateSelector x:Key="borderSelector" /&gt; &lt;DataTemplate x:Key="greenItemTemplate"&gt; &lt;Border BorderThickness="3" BorderBrush="Green"&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;/DataTemplate&gt; &lt;DataTemplate x:Key="redItemTemplate"&gt; &lt;Border BorderThickness="3" BorderBrush="Red"&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;/DataTemplate&gt; &lt;DataTemplate x:Key="yellowItemTemplate"&gt; &lt;Border BorderThickness="3" BorderBrush="Yellow"&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;/DataTemplate&gt; &lt;/Grid.Resources&gt; &lt;ListBox ItemsSource="{Binding myCollectionOfMyClassObjects}" Name="listBox1" HorizontalContentAlignment="Stretch" ItemTemplateSelector="{StaticResource borderSelector}" &gt; &lt;/ListBox&gt; </code></pre> <p></p> <p>However, although the binding works fine (the list of non-formatted objects appears in the list), the ItemTemplateSelector is not calling the TemplateSelector methods. I put a breakpoint at the beginning of the methods and it's never called.</p> <p>Does anyone knows what could be the problem?</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.
 

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