I was searching for a standard (not hacky and without binding errors) solution to this problem. And I found it here: using DataTemplateSelector
.
It's the same idea as per @H.B. answer: check whenever there is a ComboBoxItem
as a parent in visual tree.
public class ComboBoxItemTemplateSelector : DataTemplateSelector
{
public DataTemplate SelectedTemplate { get; set; }
public DataTemplate DropDownTemplate { get; set; }
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
while (container != null)
{
container = VisualTreeHelper.GetParent(container);
if (container is ComboBoxItem)
return DropDownTemplate;
}
return SelectedTemplate;
}
}
Usage:
<ComboBox.ItemTemplateSelector>
<local:ComboBoxItemTemplateSelector>
<local:ComboBoxItemTemplateSelector.SelectedTemplate>
<DataTemplate>
... simple template for selected item
</DataTemplate>
</local:ComboBoxItemTemplateSelector.SelectedTemplate>
<local:ComboBoxItemTemplateSelector.DropDownTemplate>
<DataTemplate>
... complex template used by dropdown items
</DataTemplate>
</local:ComboBoxItemTemplateSelector.DropDownTemplate>
</local:ComboBoxItemTemplateSelector>
</ComboBox.ItemTemplateSelector>