I have a ObservableCollection binding in ItemsControl. In this ItemsControl is created some TextBoxes, one for each object in ObservableCollection.
Now I need to select text and highlight it depending the selected object but I'm not able to do it.
My xaml:
<StackPanel x:Name="stackPanel">
<StackPanel.Children>
<ItemsControl x:Name="itemsControl" ItemsSource="{Binding MyCollection}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBox Text="{Binding MyContent}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel.Children>
</StackPanel>
And this is my code which is called by one event:
for (int i = 0; i < this.stackPanel.Children.Count; i++)
{
TextBox t = this.stackPanel.Children[i] as TextBox;
if (t != null)
{
// do selection
}
}
// and also in this way:
for (int i = 0; i < itemsControl.Items.Count; i++)
{
UIElement uiElement =
(UIElement)itemsControl.ItemContainerGenerator.ContainerFromIndex(i);
TextBox t2 = (uiElement as TextBox);
if (t2 != null)
{
// do selection
}
}
How can I get these textbox? Thanks.