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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:03:08+00:00 2026-05-11T14:03:08+00:00

I have a ListBox, that’s bound to an ObservableCollection . Each ListBoxItem is displayed

  • 0

I have a ListBox, that’s bound to an ObservableCollection.

Each ListBoxItem is displayed with a DataTemplate. I have a button in my DataTemplate, that when clicked, needs a reference to the member of the ObservableCollection it’s part of the DataTemplate for. I can’t use the ListBox.SelectedItem property because the item does not become selected when clicking the button.

So either: I need to figure out how to properly set ListBox.SelectedItem when the mouse hovers, or when the button is clicked. Or I need to figure out another way to get a reference to the CLR Object bound to the ListBoxItem that the button belongs to. The second option seems cleaner, but either way is probably OK.

Simplified code segment below:

XAML:

<DataTemplate x:Key='postBody'>     <Grid>         <TextBlock Text='{Binding Path=author}'/>         <Button Click='DeleteButton_Click'>Delete</Button>     </Grid> </DataTemplate>  <ListBox ItemTemplate='{StaticResource postBody}'/> 

C#:

private void DeleteButton_Click(object sender, RoutedEventArgs e) {     Console.WriteLine('Where mah ListBoxItem?'); } 
  • 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. 2026-05-11T14:03:09+00:00Added an answer on May 11, 2026 at 2:03 pm

    Generally speaking people will be interested in a CLR object directly bound to the ListBoxItem, not the actual ListBoxItem. If you had a list of posts for example you could use your existing template of:

    <DataTemplate x:Key='postBody' TargetType='{x:Type Post}'>   <Grid>     <TextBlock Text='{Binding Path=author}'/>     <Button Click='DeleteButton_Click'>Delete</Button>   </Grid> </DataTemplate> <ListBox ItemTemplate='{StaticResource postBody}'    ItemSource='{Binding Posts}'/> 

    and in your code-behind, your Button‘s DataContext is equal to your DataTemplate‘s DataContext. In this case a Post.

    private void DeleteButton_Click(object sender, RoutedEventArgs e){   var post = ((Button)sender).DataContext as Post;   if (post == null)     throw new InvalidOperationException('Invalid DataContext');    Console.WriteLine(post.author); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 243k
  • Answers 243k
  • 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 Let me begin by answering your question with a question… May 13, 2026 at 7:46 am
  • Editorial Team
    Editorial Team added an answer You can't. 2Checkout does not allow donations. https://www.2checkout.com/documentation/prohibited.html May 13, 2026 at 7:46 am
  • Editorial Team
    Editorial Team added an answer Get the number of steps. Then, do the $("#clock").load("utility.php" ...… May 13, 2026 at 7:46 am

Related Questions

I have a ListBox with a StackPanel that contains an image and label. <ListBox.ItemsPanel>
I have a ListBox on a form that is bound to an ObservableCollection of
I have a ListBox, that's bound to an ObservableCollection . Each ListBoxItem is displayed
I have a ListBox with ListBoxItem s that contains a nested ListBox . Now
I have a listbox with a datatemplate that holds a number of controls bound

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.