You could bind the AlternationCount
to the Script.Count
then add the AlternationIndex
from the ItemsControl
(ListBox) to the Textbox
Tag
property so you can access from your GotFocus
event handler.
Example:
<ListBox Name="ScriptEditor" Margin="10" Height="291" ItemsSource="{Binding Script}" AlternationCount="{Binding Script.Count}" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding ., Mode=OneWay}" GotFocus="ScriptEditor_GotFocus"
Tag="{Binding Path=(ItemsControl.AlternationIndex), Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
private void ScriptEditor_GotFocus(object sender, RoutedEventArgs e)
{
int index = (int)(sender as TextBox).Tag;
}