get the index of a button in a x:Array of a ListBox (wpf)
Hello Is there a way to get the index of a button in an x:Array of a ListBox.
I know that I can get the button info once clicked (via the sender argument) but I can't get its index.
ListBox.CurrentItem and ListBox.SelectedItem don't work
no luck with int t = this.lstButtons.SelectedIndex;
t=this.lstButtons.Items.CurrentPosition;
t=lstButtons.ItemContainerGenerator.IndexFromContainer((Button)sender);
<x:Array Type="Button" >
<Button Content="Button1" VerticalAlignment="Top" Width="149" Click="Button_Click" />
开发者_运维技巧 <Button Content="Button2" VerticalAlignment="Top" Width="149" Click="Button_Click"/>
<Button Content="Button3" BorderThickness="4" VerticalAlignment="Top" Width="149" Click="Button_Click"/>
</x:Array>
</ListBox.ItemsSource>
Well I would re-think the problem because it sounds like a pretty brittle and roundabout way to take conditional action based on the item. But what you could probably do is:
int index = Array.IndexOf((Button[])listBox.ItemsSource, clickedButton);
精彩评论