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 6607059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:27:44+00:00 2026-05-25T19:27:44+00:00

TL;DR up front: I would like to use a default HierarchicalDataTemplate for all but

  • 0

TL;DR up front: I would like to use a “default” HierarchicalDataTemplate for all but a specific few nodes in a WPF TreeView. These nodes come from an XMLDocument and are not fully known until runtime. Is there a way to do this?

At runtime, I am analyzing specific parts of a system, and building an XML document that I’m then binding to a TreeView like so:

MyTree.DataContext = MyXMLDocument;

This is my WPF declaration of the TreeView:

<TreeView x:Name="MyTree" ItemsSource="{Binding Mode=OneWay, XPath=/Analysis}" ItemTemplate="{StaticResource GenericElementWithChildren}"/>

The template starts like this…

<HierarchicalDataTemplate x:Key="GenericElementWithChildren" ItemsSource="{Binding XPath=child::node()}">

So for example, I might have some long XML document about different aspects of the analysis I just ran and I’d like some particular element like “Disk” or “Proprietary Foo Service” to have a special template because it should be displayed nicely, while everything else just gets a generic template.

I’ve thought about using a DataTemplateSelector but it seems like there must be a better way. I’m not even sure how I’d do that for XML. Do any of you smarter folks have any wisdom to impart, or am I stuck figuring out how to write a DataTemplateSelector against XML?

  • 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-05-25T19:27:45+00:00Added an answer on May 25, 2026 at 7:27 pm

    DataTemplate and HierarchicalDataTemplate also have a DataType property.
    When you remove the x:Key and supply a DataType, these Templates are implicit. So you can define your different templates as implicit and they will be used automatically, as long as you don’t supply a ItemTemplate or ItemTemplateSelector on the inital TreeView.

    <HierachicalDataTemplate DataType="{x:Type ProprietaryFooService}">
    

    Personally i try to avoid that, because this also means that other controls that can show your data are using these Templates aswell. Imo the best would be to use a DataTemplateSelector, especially when you deal with the same types that you need to show in different ways.

    Edit:
    Sorry i missed that you are using Xpath. I guess it will not work there. I will leave this answer here, but can’t guarantee that it suits your needs. Maybe it helps in another way anyway.

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

Sidebar

Related Questions

I would like to use the system font but with a custom leading, but
Simple question, I would like to append text to the front of every print
Hello I would like to use in my css simple inheritance from the browsers
I would like to create a gallery slideshow using Jquery Tools Scrollable plugin. But
I would like to display floated images with a caption underneath. But using the
I have two specific fonts installed on my computer and would like to configure
I would like gnuplot to use the same font in the postscript-terminal as it
My site is quite visual and I would like to make use of users
I would like to use the external font Impact on my android app. Currently
I would like to highlight some portions of code with a specific background color,

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.