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

  • SEARCH
  • Home
  • 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 6126333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:21:36+00:00 2026-05-23T16:21:36+00:00

I searched the archives for help but I can’t find anything quite specific enough

  • 0

I searched the archives for help but I can’t find anything quite specific enough for my particular issue.

I have a TreeView using MVVM to bind data and all seems good. I want to extend the functionality such that I think using a user control for the TreeView items would be good.

Here is the XAML code for the hierarchical data template used by the TreeViewItems:

        <HierarchicalDataTemplate
          DataType="{x:Type vm:SiteViewModel}" 
          ItemsSource="{Binding Children}">
          <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding SiteName}"/>
         </StackPanel>
        </HierarchicalDataTemplate>

I want to replace the TextBlock with my user control:

        <uc:MyTextBlock InternalText="{Binding SiteName}"/>

The user control (for now) just contains another TextBlock and has a dependency property called InternalText, i.e.

  <TextBlock Text="{Binding Path=InternalText}" />

and I set the DataContext in the constructor of the user control to itself:

public MyTextBlock ()
{
  InitializeComponent();
  DataContext = this;
}

This isn’t working, but if I just change the template so that it specifies static text it seems to work fine:

        <uc:MyTextBlock InternalText="Some site name"/>

So how do I get the bound data to get passed to the user control? It’s probably something simple but I’m new to WPF so I’ve not worked it out yet.

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-23T16:21:36+00:00Added an answer on May 23, 2026 at 4:21 pm

    In the codebehind

    public class MyUserControl : UserControl
    {
        #region Text
        /// <summary>
        /// The <see cref="DependencyProperty"/> for <see cref="Text"/>.
        /// </summary>
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.Register(
                RunningPropertyName,
                typeof(string),
                typeof(MyUserControl ),
                new UIPropertyMetadata(null));
    
        /// <summary>
        /// The name of the <see cref="Text"/> <see cref="DependencyProperty"/>.
        /// </summary>
        public const string TextPropertyName = "Text";
    
        /// <summary>
        /// The text to display
        /// </summary>
        public string Text
        {
            get { return (string)GetValue(TextProperty ); }
            set { SetValue(TextProperty , value); }
        }
        #endregion      
    }
    

    In the xaml

    <UserControl x:Class="Derp.MyUserControl" 
                 x:Name="root"
                 SnippingXamlForBrevity="true"
    

    and later…

    <TextBlock Text="{Binding Text, ElementName=root}" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've already searched the archives but can't find a solution that works for linq
Ive searched through the archived posts but I couldnt find anything similar. Just a
I've searched around but can't find a solution to this problem. I'm using the
Searched for answer for this but couldn't find anything. The closest I could find
I searched before but couldn't find any answers. I am somewhat new to c++,
I've searched but does Visual Studio 2008 have a setting for C# where it
I have searched this web looking for an answer, but it seems that this
I have searched and am not able to find information on what it is
I searched but didn't find the answer why I got error on installation: $
I've searched the archives and this question seems closest to my own. But, unfortunately

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.