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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:48:22+00:00 2026-05-27T05:48:22+00:00

I have a ListView containing only buttons. What I want to do is pretty

  • 0

I have a ListView containing only buttons. What I want to do is pretty simple, I want to have the index of the button that has been clicked. The count of the list varies from 0 to 100, so when the user clicks on button 6, I need this number for processing.

I defined my ListView like this:

<ListView Name="myListView" 
          ItemsSource="{Binding Source={StaticResource myDataModel}, 
          Path=StatusList, 
          Mode=OneWay}">
          <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"></StackPanel>
                </ItemsPanelTemplate>
          </ListView.ItemsPanel>

          <ListView.ItemTemplate>
               <DataTemplate>
                    <Button Mode=OneWay}"  
                            Click="Button_Click"/> 
                </DataTemplate>
           </ListView.ItemTemplate>
 </ListView>

My original idea was to create a custom button with an ID and bind the index to the ID but I can’t figure out how to do that.

I tried:

int a = myListView.Items.IndexOf(((Button)sender)); 

inside the event handler, but it always returns 0xffffffff
can anybody tell me how to get the index of the clicked button?

  • 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-27T05:48:23+00:00Added an answer on May 27, 2026 at 5:48 am

    This should work:

    Swap your ListView with an ItemsControl and set an AlternationCount to a very high number (higher than the max count elements in your list).
    Make a command and pass the current index as a parameter.

    XAML:

    <Window.CommandBindings>
      <CommandBinding 
       Command="Select" 
       Executed="Click_Executed" />
    </Window.CommandBindings>
    
    <ItemsControl AlternationCount="9999" Name="myListView" 
          ItemsSource="{Binding Source={StaticResource myDataModel}, 
          Path=StatusList, 
          Mode=OneWay}">
      <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
          <StackPanel Orientation="Horizontal"></StackPanel>
        </ItemsPanelTemplate>
      </ItemsControl.ItemsPanel>
    
      <ItemsControl.ItemTemplate>
        <DataTemplate>
          <Button Command="Select"
                    CommandParameter="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource TemplatedParent}}"
                    Content="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=(ItemsControl.AlternationIndex)}" Width="200" Height="20" Click="Button_Click"/>
        </DataTemplate>
      </ItemsControl.ItemTemplate>
    </ItemsControl>
    

    Code Behind:

    private void Click_Executed(object sender, ExecutedRoutedEventArgs e)
    {
      MessageBox.Show("Index: " + e.Parameter.ToString());
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ListView containing items with one button each and I want to
I have ListView that has the following EditItemTemplate: <EditItemTemplate> <tr style=> <td> <asp:LinkButton ID=UpdateButton
I have a ListView that I want to use with an ArrayAdapter to add
I have a ListView that's populated from a SimpleCursorAdapter each row containing 3 different
I have a listView with custom rows containing buttons and I populate the listView
I have a DataBaseHelper which has a fetchData() method that returns a Cursor containing
I have a view containing a ListView and an Edit Button. The ListView's ItemSource
I have a Windows Forms app, that has a single ElementHost containing a WPF
I have an activity that has a TabHost containing a set of TabSpecs each
I have a listview with each row containing some text and a delete button.

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.