Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 815
peer Avatar asked Dec 05 '11 11:12

peer


2 Answers

Use the DataContext to find the item:

var item = (sender as FrameworkElement).DataContext;
int index = myListView.Items.IndexOf(item);
like image 169
Bas Avatar answered Oct 27 '22 13:10

Bas


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());
}
like image 36
SvenG Avatar answered Oct 27 '22 13:10

SvenG