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.
Here is my DataTemplateSelector code
public class TemplateSelector : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
FrameworkElement element = container as FrameworkElement;
if (element != null && item != null && 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;
}
}
Here is my xaml
<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}">
<Grid.Resources>
<c:myConverter x:Key="converter" />
<c:TemplateSelector x:Key="borderSelector" />
<DataTemplate x:Key="greenItemTemplate">
<Border BorderThickness="3" BorderBrush="Green">
<StackPanel HorizontalAlignment="Stretch">
<TextBlock Text="{Binding Path=GroupName}"/>
<TextBlock Text="{Binding Path=myType}"/>
</StackPanel>
</Border>
</DataTemplate>
<DataTemplate x:Key="redItemTemplate">
<Border BorderThickness="3" BorderBrush="Red">
<StackPanel HorizontalAlignment="Stretch">
<TextBlock Text="{Binding Path=GroupName}"/>
<TextBlock Text="{Binding Path=myType}"/>
</StackPanel>
</Border>
</DataTemplate>
<DataTemplate x:Key="yellowItemTemplate">
<Border BorderThickness="3" BorderBrush="Yellow">
<StackPanel HorizontalAlignment="Stretch">
<TextBlock Text="{Binding Path=GroupName}"/>
<TextBlock Text="{Binding Path=myType}"/>
</StackPanel>
</Border>
</DataTemplate>
</Grid.Resources>
<ListBox ItemsSource="{Binding myCollectionOfMyClassObjects}"
Name="listBox1"
HorizontalContentAlignment="Stretch"
ItemTemplateSelector="{StaticResource borderSelector}"
>
</ListBox>
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.
Does anyone knows what could be the problem?
@Rachel is right. Your DataTemplateSelector
only gets invoked once at load pretty much and not for INPC
changes.
What you can do for what you require is use a ListBoxItemStyle
with DataTrigger
switching the Template
used
something like:
<ControlTemplate x:Key="greenItemTemplate">
<Border BorderBrush="Green"
BorderThickness="3">
<StackPanel HorizontalAlignment="Stretch">
<TextBlock Text="{Binding Path=GroupName}" />
<TextBlock Text="{Binding Path=myType}" />
</StackPanel>
</Border>
</ControlTemplate>
<ControlTemplate x:Key="redItemTemplate">
<Border BorderBrush="Red"
BorderThickness="3">
<StackPanel HorizontalAlignment="Stretch">
<TextBlock Text="{Binding Path=GroupName}" />
<TextBlock Text="{Binding Path=myType}" />
</StackPanel>
</Border>
</ControlTemplate>
<ControlTemplate x:Key="yellowItemTemplate">
<Border BorderBrush="Yellow"
BorderThickness="3">
<StackPanel HorizontalAlignment="Stretch">
<TextBlock Text="{Binding Path=GroupName}" />
<TextBlock Text="{Binding Path=myType}" />
</StackPanel>
</Border>
</ControlTemplate>
<Style x:Key="MyListBoxItemStyle"
TargetType="{x:Type ListBoxItem}">
<Setter Property="Template"
Value="{DynamicResource greenItemTemplate}" />
<Style.Triggers>
<DataTrigger Binding="{Binding myType}"
Value="c">
<Setter Property="Template"
Value="{DynamicResource redItemTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding myType}"
Value="b">
<Setter Property="Template"
Value="{DynamicResource yellowItemTemplate}" />
</DataTrigger>
</Style.Triggers>
</Style>
and usage:
<ListBox ItemContainerStyle="{StaticResource MyListBoxItemStyle}"
ItemsSource="{Binding myCollectionOfMyClassObjects}"
Name="listBox1"
HorizontalContentAlignment="Stretch" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With