In a ListView containing Buttons, how to get Index

2019-04-09 04:56发布

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?

2条回答
狗以群分
2楼-- · 2019-04-09 05:19

Use the DataContext to find the item:

var item = (sender as FrameworkElement).DataContext;
int index = myListView.Items.IndexOf(item);
查看更多
smile是对你的礼貌
3楼-- · 2019-04-09 05:24

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());
}
查看更多
登录 后发表回答