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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:57:29+00:00 2026-05-26T03:57:29+00:00

I have Listbox: <ListBox x:Name=FriendsRequestList> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation=Horizontal> <StackPanel> <TextBlock Text={Binding FullName} Foreground=#FF316DCB/>

  • 0

I have Listbox:

  <ListBox x:Name="FriendsRequestList">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <StackPanel>
                            <TextBlock Text="{Binding FullName}" Foreground="#FF316DCB"/>
                            <TextBlock Text="{Binding RequestText}" />
                            <StackPanel Orientation="Horizontal">
                                <Button Name="Accept" Content="Accept" Click="Accept_Click"  Foreground="#FF28901F" Background="#FFB4D8BA"/>
                                <Button Name="Decline" Content="Decline" Click="Decline_Click"  Foreground="#FF28901F" Background="#FFB4D8BA"/>
                            </StackPanel>
                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
  </Listbox>

And I try these in code:

  private void Accept_Click(object sender, RoutedEventArgs e)
    {
        Button clickedButton = sender as Button;
        StackPanel st1 = clickedButton.Parent as StackPanel;
        StackPanel st2 = st1.Parent as StackPanel;
        StackPanel st3 = st2.Parent as StackPanel;
        object parentControl = st3.Parent;
        object obj = FriendsRequestList.Items[3];
        int index1 = FriendsRequestList.Items.IndexOf(obj);
        int index2 = FriendsRequestList.SelectedIndex; 
        int SenderId = FriendRequests.ElementAt(index).SenderID;
        UserServices.FriendRequestAccept(this, SenderId);
        UserServices.GetRequests(this);
    }

index2 is -1, and parentControl is null. Why ListItem.SelectedIndex is -1?
And how can I know which ListItem button is clicked ?

  • 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-26T03:57:29+00:00Added an answer on May 26, 2026 at 3:57 am

    The ListBox.SelectedIndex property is probably -1 because the Button is intercepting the click event and it is not being propagated to the ListBox. Anyway, you don’t need the index to do what you’re trying to do.

    Let’s say you set the ItemsSource as follows:

    FriendsRequestList.ItemsSource = FriendRequests;
    

    Now, assuming FriendRequests is some sort of collection containing FriendRequest objects, each of which contains the properties FullName, RequestText etc., modify the click handler to

    private void Accept_Click(object sender, RoutedEventArgs e)
    {
      FriendRequest req = ( sender as Button ).DataContext as FriendRequest;
      int senderID = req.SenderID;
      ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A have listbox <ListBox> <ListBox.ItemTemplate> <DataTemplate> <DockPanel> <Button Content={Binding name_trainer} Tag={Binding idPersonTrainer} DockPanel.Dock=Top HorizontalAlignment=Center>
I have a listbox where the items contain checkboxes: <ListBox Style={StaticResource CheckBoxListStyle} Name=EditListBox> <ListBox.ItemTemplate>
I have a listbox, and I have the following ItemTemplate for it: <DataTemplate x:Key=ScenarioItemTemplate>
I have a listbox that has a datatemplate which contains multiple text boxes. I
I have a ListBox that uses a custom control as the ListBox.ItemTemplate DataTemplate. I
I have a ListBox containing an ItemTemplate made up of a StackPanel containing a
I have the following code: ListBox.DataSource = DataSet.Tables(table_name).Select(some_criteria = match) ListBox.DisplayMember = name The
I have a ListBox showing data about employees such as name, department photo, badge
I have a listbox named lbselectedcom in my web page. and i am binding
I have a ListBox (with ItemTemplate defined in the XAML), and a DataGrid. 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.