peer
peer

Reputation: 364

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

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?

Upvotes: 5

Views: 13499

Answers (3)

Nicolas Barajas
Nicolas Barajas

Reputation: 31

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);
}

Upvotes: 0

Bas
Bas

Reputation: 27115

Use the DataContext to find the item:

var item = (sender as FrameworkElement).DataContext;
int index = myListView.Items.IndexOf(item);

Upvotes: 31

SvenG
SvenG

Reputation: 5205

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());
}

Upvotes: 2

Related Questions