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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:50:59+00:00 2026-05-27T02:50:59+00:00

I need to add images to WPF treeview nodes, I’ve had a look at

  • 0

I need to add images to WPF treeview nodes, I’ve had a look at this example How do I add icons next to the nodes in a WPF TreeView? and it’s working fine except ALL nodes have the same image.I would like all the nodes in the treeview which do not have any children to either have no image or have a different image.

Here is my XAML where I set the image:

   <HierarchicalDataTemplate x:Key="NodeTemplate">
        <StackPanel Orientation="Horizontal" Margin="2">
            <Image Source="test.png"  Width="16" Height="16" SnapsToDevicePixels="True"/>
        <TextBlock x:Name="tb"/>
        </StackPanel>
        <HierarchicalDataTemplate.ItemsSource>
            <Binding>
                <Binding.XPath>child::node()</Binding.XPath>
            </Binding>
        </HierarchicalDataTemplate.ItemsSource>
        <HierarchicalDataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=NodeType}" Value="Text">
                <Setter TargetName="tb" Property="Text" Value="{Binding Path=Value}"></Setter>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
                <Setter TargetName="tb" Property="Text" Value="{Binding Path=Name}"></Setter>
            </DataTrigger>
        </HierarchicalDataTemplate.Triggers>
    </HierarchicalDataTemplate>

Below is a screen shot of the output

Treeview output
Could someone please suggest how can I achieve this, the possible solution can be either changing the XAML or programatically via C#.

  • 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-27T02:50:59+00:00Added an answer on May 27, 2026 at 2:50 am

    Here is some code I used to solve almost the same problem.
    (The data I designed this for is XML data, so XPath=”@name” means the value of the attribute name of the node, while Name means element type.)

    <Window x:Class="NodeExplorer2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:NodeExplorer2">
        <Window.Resources>
            <my:PathConverter x:Key="iconConverter"/>
    
            <HierarchicalDataTemplate x:Key="XmlTreeTemplate">
                <HierarchicalDataTemplate.ItemsSource>
                    <Binding XPath="child::node()" />
                </HierarchicalDataTemplate.ItemsSource>
    
                <StackPanel Orientation="Horizontal">
                    <Image x:Name="icon" SnapsToDevicePixels="True" Stretch="None" Margin="0,0,3,0" />
                    <TextBlock Text={Binding XPath="@name"/>
                </StackPanel>
                <HierarchicalDataTemplate.Triggers>
                    <DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
                        <Setter TargetName="icon" Property="Source">
                            <Setter.Value>
                                <Binding Path="Name" Converter="{StaticResource iconConverter}">
                                    <Binding.FallbackValue>
                                        <ImageSource>
                                            Data/Icons/unknown.png
                                        </ImageSource>
                                    </Binding.FallbackValue>
                                </Binding>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </HierarchicalDataTemplate.Triggers>
            </HierarchicalDataTemplate>
    

    Converter:

    public class PathConverter: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            //Console.WriteLine("Value:" + value);
                return "Data/Icons/" + value + ".png";
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return "";
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to add DVD writing functionality to an application I'm working on. However
I'm looking for something to add tags on images in WPF. Is there some
I'm working on a WPF application right now in C#, and I need to
My App need to update the status and add images to the update, so
I am trying to fill a treeview in WPF with icons of files and
I need to read some tiff files into a WPF, and I had it
I need to add more png images to my app. When the size of
I need to add data to a set of images that increments after each
I need to add specific padding around large images and the current method I
I need to add images to a mail message and need them to be

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.