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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:13:57+00:00 2026-06-01T17:13:57+00:00

I have two classes, which reference the third: class Data1 { public Named Xxx

  • 0

I have two classes, which reference the third:

class Data1
{
   public Named Xxx { get; set; }
   public SomeClass1 Foo { get; set; }
   ...
}

class Data2
{
   public Named Yyy { get; set; }
   public SomeClass2 Bar { get; set; }
   ...
}

class Named
{
   public string Name { get; set; }
   ...
}

Now, I would like to display both Data1 & Data2:

<TreeView DataContext={Binding Path=Data1}>
  <TreeView.Items>
    <TreeViewItem>
      <TreeViewItem.Header>
        <StackPanel Orientation="Horizontal">
          <ContentControl xml:space="preserve">Name: </ContentControl>
          <ContentControl Content="{Binding Path=Xxx.Name}" />
        </StackPanel>
      </TreeViewItem.Header>
    </TreeViewItem>
    <TreeViewItem><!-- somehow display Foo --></TreeViewItem>
    <!-- More TreeViewItems, specific to Data1 -->
  </TreeView.Items>
</TreeView>

<TreeView DataContext={Binding Path=Data2}>
  <TreeView.Items>
    <TreeViewItem>
      <TreeViewItem.Header>
        <StackPanel Orientation="Horizontal">
          <ContentControl xml:space="preserve">Name: </ContentControl>
          <ContentControl Content="{Binding Path=Yyy.Name}" />
        </StackPanel>
      </TreeViewItem.Header>
    </TreeViewItem>
    <TreeViewItem><!-- somehow display Bar --></TreeViewItem>
    <!-- More TreeViewItems, specific to Data2 -->
  </TreeView.Items>
</TreeView>

So, markup is different, except for TreeViewItem that displays Named class. I would like to reuse markup for this TreeViewItem. It is too simple to make UserControl of it, but it is still a little more complicated than shown in example. So, I would really like to do something like this:

<ResourceDictionary>
  <TreeViewItem x:Key="Named">
    <TreeViewItem.Header>
      <StackPanel Orientation="Horizontal">
        <ContentControl xml:space="preserve">Name: </ContentControl>
        <ContentControl Content="{Binding Path=Name}" />
      </StackPanel>
    </TreeViewItem.Header>
  </TreeViewItem>
</ResourceDictionary>

And then just use it like this:

<TreeView DataContext={Binding Path=Data1}>
  <TreeView.Items>
    <StaticResource ResourceKey="Named" />
  </TreeView.Items>
</TreeView>

As you can see, Data1‘s property name for Named is Xxx, while Data2‘s property name for Named is Yyy. So, I have to pass that somehow to my resource. But how?

E.g. how do I set the DataContext of this StaticResource subtree to Xxx for Data1?

Something like this doesn’t work:

    <StaticResource ResourceKey="Named" DataContext={Binding Path=Xxx} />

Sorry for the long question.

Edit:

All I want is a piece of XAML, capable of displaying a Named instance. So, I want to be able to specify where to get the instance (from Xxx or Yyy) outside of this piece, so I can reuse it.

Edit2: here is the solution with ControlTemplate, however it doesn’t work well: the TreeViewItem becomes unselectable. What’s wrong?

<ControlTemplate x:Key="Named" TargetType="TreeViewItem">
    <TreeViewItem>
        <TreeViewItem.Header>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Name: " />
                <TextBlock Text="{Binding Path=Name}" />
            </StackPanel>
        </TreeViewItem.Header>
    </TreeViewItem>
</ControlTemplate>

<!-- now use the template: -->
<TreeView>
  <TreeView.Items>
    <TreeViewItem Template="{StaticResource Named}" 
                  DataContext="{Binding Path=Xxx}" />
    ...
  • 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-01T17:13:58+00:00Added an answer on June 1, 2026 at 5:13 pm

    OK, I took a nap today and when I woke up, I suddenly realized what the answer is! Here it is:

    <!-- In resources: -->
    <Style TargetType="TreeViewItem" x:Key="Named">
      <Setter Property="Header" Value="{Binding Name}" />
      <Setter Property="HeaderTemplate">
        <Setter.Value>
          <DataTemplate>
            <StackPanel Orientation="Horizontal">
              <TextBlock Text="Name: " />
              <TextBlock Text="{Binding}" />
            </StackPanel>
          </DataTemplate>
        </Setter.Value>
      </Setter>
    </Style>
    
    <!-- now use the template: -->
    <TreeView>
      <TreeView.Items>
        <TreeViewItem Style="{StaticResource Named}" 
                      DataContext="{Binding Path=Xxx}" />
        ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that instantiates two classes which implement interfaces. I want one
I have two async classes in my application which you can see below class
I have a MustInherit Parent class with two Child classes which Inherit from the
I have an abstract class Airplane, and two classes PassengerAirplane and CargoAirplane, which extend
I have a class which has two HashSet<String> collections as private members. Other classes
i have created two classes Standard and Family which extend abstract class Room ,
I have two classes in short here they are: public final class ServerMain {
what i have is two classes //class A is a kind of config-class which
I have two classes: public class CourseModule { // attributes... List<Course> courses; public void
i have two data classes which hold only data members(no functions). One is CallTask

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.