none
HierarchicalDataTemplateSelector

    Question

  • Hi,

    I want to use a selector for my treeviewitem. I have 5 HierarchicalDataTemplates and i want to choose at run time depending upon the data which HierarchicalDataTemplateto apply. I tried using DataTemplateSelector but i guess seeing its return type as DataTemplate the treeview does not render properly. Any help will be greatly appricaited.

    Thanks

    Zepher


    Zepher684
    Friday, April 16, 2010 8:56 AM

Answers

  • Hi Zepher,

    You can define a DataTemplateSelector for tree view item, and in the override of the SelectTemplate method, we will return the correct hierarchical data template according to the item type.
    Here is a simple solution as follows:
    XAML

    <Window.Resources>

        <local:MyTemplateSelector x:Key="myTempalteSelecrot"></local:MyTemplateSelector>

        <HierarchicalDataTemplate x:Key="familyTemplate" DataType="{x:Type local:Family}" ItemsSource="{Binding Path=Members}">

            <TextBlock Text="{Binding Path=FamilyName}"></TextBlock>

        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate DataType="{x:Type local:Person}" x:Key="personTemplate" ItemsSource="{Binding Path=Traints}">

            <TextBlock>

                <TextBlock Text="{Binding Path=Name}"></TextBlock>

                (<TextBlock Text="{Binding Path=Age}"></TextBlock>)

            </TextBlock>

        </HierarchicalDataTemplate>

    </Window.Resources>

    <StackPanel>

        <TreeView DataContext="{StaticResource families1}">

            <TreeViewItem ItemsSource="{Binding}" Header="Families" ItemTemplateSelector="{StaticResource myTempalteSelecrot}"></TreeViewItem>

        </TreeView>

    </StackPanel>

     

    Code-behind

    public class MyTemplateSelector : DataTemplateSelector

    {

        public override DataTemplate SelectTemplate(object item, DependencyObject container)

        {

            if (item != null)

            {

                Window window = Application.Current.MainWindow;

                if (item is Family)

                {

                    return window.FindResource("familyTemplate") as DataTemplate;

                }

                else if (item is Person)

                {

                    return window.FindResource("personTemplate") as DataTemplate;

                }

                else

                {

                    return null;

                }

            }

            return null;

        }

    }

     

    Hope this helps.

    Best regards,
    Linda Liu


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Linda Liu Friday, April 23, 2010 3:24 AM
    Tuesday, April 20, 2010 6:31 AM

All replies

  • Hi Zepher,

    You can define a DataTemplateSelector for tree view item, and in the override of the SelectTemplate method, we will return the correct hierarchical data template according to the item type.
    Here is a simple solution as follows:
    XAML

    <Window.Resources>

        <local:MyTemplateSelector x:Key="myTempalteSelecrot"></local:MyTemplateSelector>

        <HierarchicalDataTemplate x:Key="familyTemplate" DataType="{x:Type local:Family}" ItemsSource="{Binding Path=Members}">

            <TextBlock Text="{Binding Path=FamilyName}"></TextBlock>

        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate DataType="{x:Type local:Person}" x:Key="personTemplate" ItemsSource="{Binding Path=Traints}">

            <TextBlock>

                <TextBlock Text="{Binding Path=Name}"></TextBlock>

                (<TextBlock Text="{Binding Path=Age}"></TextBlock>)

            </TextBlock>

        </HierarchicalDataTemplate>

    </Window.Resources>

    <StackPanel>

        <TreeView DataContext="{StaticResource families1}">

            <TreeViewItem ItemsSource="{Binding}" Header="Families" ItemTemplateSelector="{StaticResource myTempalteSelecrot}"></TreeViewItem>

        </TreeView>

    </StackPanel>

     

    Code-behind

    public class MyTemplateSelector : DataTemplateSelector

    {

        public override DataTemplate SelectTemplate(object item, DependencyObject container)

        {

            if (item != null)

            {

                Window window = Application.Current.MainWindow;

                if (item is Family)

                {

                    return window.FindResource("familyTemplate") as DataTemplate;

                }

                else if (item is Person)

                {

                    return window.FindResource("personTemplate") as DataTemplate;

                }

                else

                {

                    return null;

                }

            }

            return null;

        }

    }

     

    Hope this helps.

    Best regards,
    Linda Liu


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Linda Liu Friday, April 23, 2010 3:24 AM
    Tuesday, April 20, 2010 6:31 AM
  • Hi Liu,

    Thanks for the reply. I already did the same and it worked like charm. My first assumption of there being an issue due to the returm type of DataTemplateSelector was completely wrong, my apologies for the same. It was actually unable to find a style resource and hence the issue.

     

    Thanks

    Zepher


    Zepher684
    Friday, April 23, 2010 5:36 AM