Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8963217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:18:40+00:00 2026-06-15T16:18:40+00:00

I’m new to WPF so may be missing something obvious but haven’t been able

  • 0

I’m new to WPF so may be missing something obvious but haven’t been able to find an answer anywhere.

Basically I’m building a custom tree collection that sorts in a particular way and the nodes of the tree are typed to the type of the collection. When testing in a TreeView the HierarchicalDataTemplate doesn’t work when the nodes are generic. If they’re not generic it’s fine.

Here’s a simple node class to illustrate:

public class SimpleNode<T>
{
    private List<SimpleNode<T>> _children;
    private string _name;

    public SimpleNode(string name)
    {
        _name = name;
    }

    public List<SimpleNode<T>> Children
    {
        get
        {
            return _children;
        }
        set
        {
            _children = value;
        }
    }

    public string Name
    {
        get
        {
            return _name;
        }
    }
}

then in the xaml:

HierarchicalDataTemplate DataType="{x:Type local:SimpleNode`1}" ItemsSource="{Binding Children}"

Apparently the `1 appended to the type is a result of SimpleNode having 1 generic param. If I strip the class of generics the HierarchicalDataTemplate works and I can view the tree in the TreeView. With the generics in place it won’t do it.

Any ideas?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:18:41+00:00Added an answer on June 15, 2026 at 4:18 pm

    There is no support for that out of the box. But you can make it work by creating your own DataTemplateSelector that supports generics:

    public class OpenGenericTypeDataTemplateSelector : DataTemplateSelector
    {
        public override DataTemplate SelectTemplate(object item,
                                                    DependencyObject container)
        {
            var element = container as FrameworkElement;
            if (item != null && element != null)
            {
                if (item.GetType().IsGenericType)
                {
                    var genericTypeDefinition = item.GetType()
                                                    .GetGenericTypeDefinition();
                    var key = new DataTemplateKey(genericTypeDefinition);
                    return element.TryFindResource(key) as DataTemplate;
                }
            }
    
            return null;
        }
    }
    

    You than can use it like this:

    <UserControl.Resources>
      <OpenGenericTypeDataTemplateSelector x:Key="GenericTemplateSelector" />
    </UserControl.Resources>
    ...
    <TreeView ItemTemplateSelector="{StaticResource GenericTemplateSelector}" ...>
      <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type local:SimpleNode`1}"
                                  ItemsSource="{Binding Children}">
          ...
        </HierarchicalDataTemplate>
      </TreeView.Resources>
    

    What that does is actually very simple:
    When you create a DataTemplate for a certain type, that type’s name will automatically become the key of that template.
    The default DataTemplateSelector uses the type of the item as lookup value. But the type of a an instance of a generic class is a closed generic type, i.e. one that has the generic type parameter specified.
    Problem is: The template has been registered for the open generic type, i.e. without the generic type parameter specified. That’s why the default DataTemplateSelector doesn’t pick up DataTemplates registered for an open generic type.
    The implementation I provided adds support for open generic types by simply checking the type of the item. If it is generic, we get ourselves the open generic type (via GetGenericTypeDefinition) and use that as the key.

    To actually make use of this selector, we have to tell the TreeView that it should use it. We are doing that by specifying the ItemTemplateSelector.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.