DataTemplateSelector does not work if ItemContainerStyle is set
I have two lists, default and custom. One is using the DataTemplateSelector correctly, and the other is simply using the standard DataTemplates that never call the selector;
//shows correctly
<ListBox Name="testlb" ItemTemplateSelector="{StaticResource ffDataTemplateSelector}"/>
//now showing correctly (using default DataTemplates instead of selector)
<local:FFBox x:Name="myFFBox" ItemTemplateSelector="{StaticResource ffDataTemplateSelector}" ItemContainerStyle="{StaticResource FFItemStyle}" />
Both have the same source
testlb.ItemsSource = CollectionViewSource.GetDefaultView(FileCollectionView);
myFFBox.ItemsSource = CollectionViewSource.GetDefaultView(FileCollectionView);
Obviously there is nothing wrong with the DataTemplateSelector as it works correctly on testlb
The problem ItemContainerStyle="{StaticResource FFItemStyle}"
I am using is to define the overall look of each ListBoxItem contains triggers, animations, etc. If present, the selector does not work.
<Style x:Key="FFItemStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid x:Name="mygrid">
<ContentPresenter x:Name="ContentPresenter"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"/>
How can I keep the ItemContainerStyle and still be able to modify the DataTamplates using the DataTemplateSelector?
EDIT: Solved, we have to keep it as it is one of those illogical and not quite documented things in wpf.
source to share
Got it: If you have ItemContainerStyle defined, instead of ItemTemplateSelector you need to use ContentTemplateSelector in ContentPresenter using DataTemplateSelector.
<ContentPresenter x:Name="ContentPresenter" Content="{TemplateBinding Content}"
ContentTemplateSelector="{StaticResource ffDataTemplateSelector}"
source to share
mmm code looks ok, but you tried to set ItemTemplateSelector like this.
in Generic.xaml
<Setter Property="ItemTemplateSelector">
<Setter.Value>
<helpers:ffDataTemplateSelector x:Name="ffDataTemplateSelector" />
</Setter.Value>
</Setter>
where helpers are installed the same way at the top of Generic.xaml
xmlns:helpers="using:[Namespace where ffDataTemplateSelector resides]"
source to share