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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:28:54+00:00 2026-05-18T09:28:54+00:00

I have a Listbox filled with Buttons. When the buttons is clicked then a

  • 0

I have a Listbox filled with Buttons. When the buttons is clicked then a new Page shows. But i wanna have the selected index of the listbox when i click on the button. But the listboxitem is never selected. Is there a way to get the index where the button lies by clicking the button. Thanks for help.

Example code

 <ListBox>
   <ListBox.ItemTemplate>
        <DataTemplate>
              <Button Click="ViewDetail_Click" Content="{Binding Path=Area}"></Button>
         </DataTemplate>
    </ListBox.ItemTemplate>
  </ListBox>
  • 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-18T09:28:55+00:00Added an answer on May 18, 2026 at 9:28 am

    The data context of the button will contain the source data item corresponding to the list item. You’ve not shown what your ItemsSource is for the list, so I can’t show exactly what code you’d need. But something like this:

    ObservableCollection<MyDataItem> items = new ObservableCollection<MyDataItem>();
    ...
    myList.ItemsSource = items;
    ...
    private void ViewDetail_Click(object sender, RoutedEventArgs e)
    {
        Button b = (Button) sender;
        MyDataItem detailItem = (MyDataItem) b.DataContext;
        int itemIndex = items.IndexOf(detailItem);
    }
    

    Of course, if the reason you’re asking for the index in the first place is because you want to get hold of the corresponding data item, you don’t actually need the index at all – the item’s already right there in the data context.

    By the way, I wouldn’t use a ListBox here. The main thing ListBox brings to the party is selection, but by putting buttons in as the items, you’re defeating that – as you’ve already discovered, the button is handling the mouse input before the list box has a chance to, meaning that you can’t actually select the items. At which point, why have a ListBox? A base ItemsControl probably makes more sense. (Wrapped in a ScrollViewer if you need scrolling.)

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

Sidebar

Related Questions

i have ListBox on my xaml page called MainListBox. i can get index that
I have ListBox and i need set background Transparent. I've selected in red, then
I have listbox and on click event I open new panel where i change
I have a Listbox that is filled with 30,000 elements <ListBox Name=lbWordlist Grid.Row=1 Margin=10
I have a WPF Listbox filled with children that fire events. Now, I am
I have a listbox that is dynamically filled by certain values depending on what
I have listbox, button, and textbox controls in a Windows application. How can I
I have a ListBox that can have multiple items selected at once. I have
I have a ListBox or DataGrid filled with thousands of entries. I would like
A have ListBox and 4 Items. 2 visible 2 colpased: Click: -this bad! I

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.