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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:44:33+00:00 2026-06-01T17:44:33+00:00

I hava a ListBox and Binding Students,I want to get ID when I Click

  • 0

I hava a ListBox and Binding Students,I want to get “ID” when I Click “Btn”. But, I don’t know how to do it.Please tell me how to do?

XAML:

        <ListBox x:Name="listBox">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="12">
                        <TextBlock Text="{Binding Name,StringFormat=Name:\{0\}}"
                                   Foreground="Orange"/>
                        <TextBlock Text="{Binding Age,StringFormat=Age:\{0\}}"
                                   Foreground="Gray"/>
                        <Button Content="Get ID Of Student"
                                x:Name="Btn"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
       </ListBox>

C#:

        List<Student> students = new List<Student> 
        {
            new Student{Name = "st1",Age = 20,ID = 1},
            new Student{Name = "st2",Age = 18,ID = 2},
            new Student{Name = "st3",Age = 21,ID = 3},
        };

        listBox.ItemsSource = students;

        public class Student 
        {
               public string Name { get; set; }
               public int Age { get; set; }
               public int ID { get; set; }  
        }
  • 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-06-01T17:44:34+00:00Added an answer on June 1, 2026 at 5:44 pm

    Bind the ID field to the CommandParameter property on the button

    <Button Content="Get ID Of Student" 
            x:Name="Btn" 
            CommandParameter="{Binding ID}" 
            ... />
    

    Then your Command will automatically get passed the ID as a parameter

    If you’re using the Click event instead of the Command property, you can cast the sender as a Button and check it’s CommandParameter, or you can cast it’s DataContext as a Student object and get the ID from that

    void Btn_Click(object sender, EventArgs e)
    {
        Button b = sender as Button;
    
        int id = (int)b.CommandParameter;
    
        // or 
        Student student = (Student)b.DataContext;
        int id = student.ID;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I hava the sql as below: select a.dept, a.name from students a group by
I hava a Transaction datatable with Amount field. I want to get Sum (Amount)
I hava a column name which is a varchar I want to filter all
I hava a CSV file that I want to treat as source code. Essentially
i hava an xml file like this: <name> <entry> <date>2012-03-18 13:53:23</date> <ID>1</ID> <category>questions</category> <question>who
i want to hava a RibbonGallery in my menue (like this ). This is
i hava a class that hold a list for each request but each request
I hava a basic Hibernate/JPA question. I want to find a best practice solution
I hava a generic class called ArrayBag. I want to override equals method. So
I hava 2 listvies on the same activity list1 and list2. I want to

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.