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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:50:33+00:00 2026-05-25T09:50:33+00:00

I have a listbox that uses a data template. The template is very simple

  • 0

I have a listbox that uses a data template. The template is very simple and consists of an Image and a TextBlock. They are both bound to an XML document. The template is as follows:

<Window.Resources>
    <XmlDataProvider x:Key="NinjaData" Source="\Data\Ninjas.xml" XPath="/Ninjas"/>                      
    <DataTemplate x:Key="NinjaDataTemplate">        
        <StackPanel>                
            <StackPanel Orientation="Horizontal" Margin="0">
                <Image Source="{Binding XPath=ImageFile}" Margin="5" Width="50"/>
                <TextBlock Text="{Binding XPath=@Name}" Margin="5" FontFamily="OCR A Std" Foreground="#FF9114" FontSize="14" Name="NinjaName"/>                   
            </StackPanel>
        </StackPanel>        
    </DataTemplate>
</Window.Resources>

The Listbox is as follows:

<ListBox Background="{x:Null}" BorderBrush="{x:Null}"
                ItemsSource="{Binding Source={StaticResource NinjaData}, XPath=Ninja}"
                ItemTemplate="{StaticResource NinjaDataTemplate}"
                >   
                    <ListBox.Resources>
                        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Gray"></SolidColorBrush>                                                      
                    </ListBox.Resources>
                    <ListBox.Effect>
                        <DropShadowEffect ShadowDepth="1" Color="#FF282828"/>
                    </ListBox.Effect>
                    </ListBox>

I’m having trouble working out how to retrieve the content of the TextBlock inside the data template when I click on it in the listbox. I want to display it in a label somewhere else on the page, but I can’t work out how to access that textblock.

Can anyone help and point me in the right direction? I feel like I’m going round in circles at the moment.

Thanks for any help.

  • 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-25T09:50:33+00:00Added an answer on May 25, 2026 at 9:50 am

    As you have a ListBox you can use the selection, just name the ListBox and bind to the SelectedItem:

    <ListBox Name="lb" ... />
    
    <Label DataContext="{Binding SelectedItem, ElementName=lb}"
           Content="{Binding XPath=@Name}"/>
    

    This does not retrieve the Text of the TextBlock in the template but gets it from the same source as the template. (You could actually target the TextBlock in the Template itself but i would not recommend it)

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

Sidebar

Related Questions

I have a VERY simple windows form that the user uses to manage Stores.
I have a listbox that uses a UserControl as the item template. Inside the
I have a ListBox that uses DataTemplateSelector to dynamically decide what template to use
I have a ListBox that is bound to a list of Persons. I want
I have a ListBox that displays a number of usercontrols that are bound to
I currently have a listbox that has its selected item bound to a property
I have a simple usercontrol that uses a simple custom panel where I just
I have a ListBox that uses a WrapPanel for its ItemsPanel , a custom
I have a very simple WPF user control that is mixed in with a
I have a ListBox that is bound to a List. Every-time the ListBox updates

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.