How to get ItemTemplateSelector to work in WPF

775 Views Asked by At

In a WPF project I have a ComboBox to select from different objects. Using an ItemsControl and it's ItemTemplateSelector I am trying to show different UI for the ComboBox selection based on a property of the object. So, in the example below we pick from person objects. In the ItemTemplateSelector we pick a different DataTemplate based on the Person's IsManager property. The trouble is it doesn't work.

I have a suspicion it might be due to the ItemsSource of the ItemsControl being bound to one item, but I am not sure. If this is the problem, what changes can I make to the code to achieve this?

XAML :

<Window x:Class="ItemsSelector.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ItemsSelector"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    
    <Grid.Resources>
        <local:Selector x:Key="selector"/>

        <DataTemplate x:Key="managerTemplate">
            <TextBlock Text="Manager"/>
        </DataTemplate>
        
        <DataTemplate x:Key="juniorTemplate">
            <TextBlock Text="Junior"/>
        </DataTemplate>
        
    </Grid.Resources>
    
    <ComboBox x:Name="cbo" Margin="2" Grid.Row="0" ItemsSource="{Binding .}" SelectedIndex="0">
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}"/>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>

    <ItemsControl Grid.Row="1" ItemTemplateSelector="{StaticResource selector}" ItemsSource="{Binding ElementName=cbo ,Path=SelectedItem}">

    </ItemsControl>
</Grid>

CODE BEHIND:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        DataContext = new Person[] {
            new Person() { Name = "Boss", IsManager = true },
            new Person() { Name = "Underling", IsManager = false }
        };
    }
}

PERSON:

public class Person
{
    public string Name { get; set; }
    public bool IsManager { get; set; }
    public string Title { get; set; }
}

SELECTOR:

public class Selector : DataTemplateSelector
{
    public override DataTemplate
        SelectTemplate(object item, DependencyObject container)
    {
        FrameworkElement element = container as FrameworkElement;

        if (element != null && item != null && item is Person)
        {

            var person = item as Person;

            switch (person.IsManager)
            {
                case true:
                    return element.FindResource("managerTemplate") as DataTemplate;

                case false:
                    return element.FindResource("juniorTemplate") as DataTemplate;

                default:
                    break;
            }
        }

        return null;
    }
}
2

There are 2 best solutions below

0
On BEST ANSWER

The ItemsSource property of an ItemsControl can only be bound to a collection that returns an IEnumerable.

You should use a ContentControl to be able to bind display the selected item of the ComboBox:

<ContentControl Grid.Row="1" ContentTemplateSelector="{StaticResource selector}" 
                Content="{Binding ElementName=cbo ,Path=SelectedItem}">
</ContentControl>
0
On

I think I've found the solution. I need to be using ContentTemplateSelector.