I have Itemcontrol in WPF. And Items source using Binding property.Items are only one button How can I show index of item when I clicked Button? I read about AlternationIndex but only show on the xaml page.
Asked
Active
Viewed 81 times
-1
-
Please help me. I tried 4 days. – Shammy Nurgeldi Jul 16 '22 at 06:51
-
You **need** to add your code to your question and be more descriptive. – Lee Taylor Jul 16 '22 at 14:51
1 Answers
0
You can pass it via Tag property.
<ItemsControl AlternationCount="10">
<ItemsControl.Items>
<Button Tag="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource Mode=Self}}" Click="Button_Click"/>
<Button Tag="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource Mode=Self}}" Click="Button_Click"/>
<Button Tag="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource Mode=Self}}" Click="Button_Click"/>
<Button Tag="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource Mode=Self}}" Click="Button_Click"/>
<Button Tag="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource Mode=Self}}" Click="Button_Click"/>
</ItemsControl.Items>
</ItemsControl>
Then you can parse it in Click handler.
//UserControl.xaml.cs
public void Button_Click(object sender, EventArgs args)
{
var button = sender as Button;
var alternationIndex = (int)Button.Tag;
}

Anton
- 718
- 6
- 11