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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:23:31+00:00 2026-05-14T04:23:31+00:00

I am binding a treeview using a Hierarchical class structure as follows. Store ->ImagePath

  • 0

I am binding a treeview using a Hierarchical class structure as follows.

Store
->ImagePath
->List
->List

When I am creating the DataTemplate for the Person, I want to use a combination of the person.name
and the image path declared in the Store.
Here is the code behind of my MainWindow.xaml file.
` public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

        Customers customers = new Customers();
        customers.Users = new List<Person> 
        { 
            new Person { Name = "John"},
            new Person { Name = "Adam"}, 
            new Person { Name = "Smith"}
        };

        Store root = new Store();
        root.ImagePath = "imageone.png";
        root.Add(customers);
        this.DataContext = root;
    }
}


public class Store : ObservableCollection<Customers> 
{
    public string ImagePath
    {
        get;
        set;
    }
} 
public class Customers
{
    public string Label
    {
        get
        {
            return string.Format("People({0})", Users.Count());
        }
    }
    public List<Person> Users
    {
        get;
        set;
    } 
}
public class Person
{
    public string Name
    {
        get;
        set;
    }
}`

and here is the xaml and this Source=”{Binding Store.ImagePath}” is not working.

    <Window.Resources >
   <DataTemplate DataType="{x:Type local:Person}" x:Key="personKey" >
        <StackPanel Orientation="Horizontal" >
            <Image Source="{Binding Store.ImagePath}"></Image>
            <TextBlock Text="{Binding Name}" />
        </StackPanel>
    </DataTemplate>
    <HierarchicalDataTemplate x:Key="customerKey" ItemsSource="{Binding Users}" ItemTemplate="{StaticResource personKey }" >
        <TextBlock Text="{Binding Label}" FontWeight="Bold"/>
    </HierarchicalDataTemplate>
</Window.Resources>
<Grid>
    <Canvas>
        <Button HorizontalAlignment="Left" DockPanel.Dock="Top" Height="29" Width="112" Canvas.Left="123" Canvas.Top="5">Image one</Button>  <Button HorizontalAlignment="Left" VerticalAlignment="Top" DockPanel.Dock="Top" Height="28" Width="119" Canvas.Left="249" Canvas.Top="7">Image two</Button>
        <TreeView  HorizontalAlignment="Stretch"  Name="treeView1" VerticalAlignment="Stretch" 
               ItemsSource="{Binding .}" ItemTemplate="{StaticResource customerKey}" Height="260" Width="363" Canvas.Left="81" Canvas.Top="45" />
    </Canvas>
</Grid>

I would also like to change the image programaticaaly and all person treeview items change when I click on of the buttons.

Thanks

  • 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-14T04:23:31+00:00Added an answer on May 14, 2026 at 4:23 am

    The DataTemplate personKey is going to get bound to a Person object (because it’s the ItemTemplate for the customerKey HierarchicalDataTemplate, whose ItemsSource is the Users collection). As always in WPF, the local DataContext overrides the inherited DataContext, and the DataContext within a DataTemplate is always the object for which the DataTemplate is being materialised.

    So the binding path Store.ImagePath is being resolved relative to the Person being displayed in the DataTemplate. But Person doesn’t have a Store property, so the binding fails.

    A quick but rather kludgy way to refer to window-level properties is by using a RelativeSource binding:

    <Image Source="{Binding Path=DataContext.Store.ImagePath,
                            RelativeSource={RelativeSource AncestorType={x:Type Window}}}" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 343k
  • Answers 343k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I see that you are missing FROM clause in your… May 14, 2026 at 5:29 am
  • Editorial Team
    Editorial Team added an answer Typically, the mapview being animated has its userInteractionEnabled property set… May 14, 2026 at 5:29 am
  • Editorial Team
    Editorial Team added an answer You would just join to the team table multiple times:… May 14, 2026 at 5:29 am

Related Questions

I am trying to make a WPF Application containing a treeview, whose data will
I am trying to populate a treeview using mvvm but the tree does not
I'm sure there is a simple answer to this but I can't seem to
I have a user control that i am using inside a DataTemplate , this
I have a treeview in wpf that is built using the xaml below. It

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.