In a ListView containing Buttons, how to get Index of the clicked one?

5

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?

wpf
listview
button
asked on Stack Overflow Dec 5, 2011 by peer • edited Dec 5, 2011 by Dave Clemmer

3 Answers

30

Use the DataContext to find the item:

var item = (sender as FrameworkElement).DataContext;
int index = myListView.Items.IndexOf(item);
answered on Stack Overflow Dec 5, 2011 by Bas • edited Dec 5, 2011 by H.B.
2

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());
}
answered on Stack Overflow Dec 5, 2011 by SvenG • edited Dec 5, 2011 by SvenG
0

This could work:

private void Button_Click(object sender, EventArgs e)
{
    Button button = (Button)sender;
    MyDataModelElementClass myDataModelElementClass = (MyDataModelElementClass)button.BindingContext;
    var index = myDataModel.IndexOf(myDataModelElementClass);
}
answered on Stack Overflow Feb 3, 2021 by Nicolas Barajas

User contributions licensed under CC BY-SA 3.0