listbox:accèder au ième élément
Bonjour,
J'ai construit une listbox avec un DataTemplate sur 2 niveaux (un stackpanel puis un button):
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
<Window.Resources>
<DataTemplate x:Key="DataTemplate_Level2">
<Button Name="btn" Content="{Binding}" Height="90" Width="115" Margin="4,4,4,4" Click="sign">
<Button.Background>
<ImageBrush ImageSource="/images/vide.jpg"></ImageBrush>
</Button.Background>
</Button>
</DataTemplate>
<DataTemplate x:Key="DataTemplate_Level1">
<ItemsControl ItemsSource="{Binding}" ItemTemplate="{DynamicResource DataTemplate_Level2}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Name="sp" Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</DataTemplate>
</Window.Resources>
<Grid Margin="0,149,0,154">
<TextBlock x:Name="t_exam" Margin="8,-151,-8,143" RenderTransformOrigin="0.5,0.5" HorizontalAlignment="Center" FontWeight="Bold">Examen</TextBlock>
<Button Click="button1_Click" Margin="430,146,0,-154" Visibility="Hidden">Connect and Getcard UID</Button>
<ScrollViewer Margin="0,-116,10,-154" VerticalScrollBarVisibility="Auto">
<ListBox Name="lst" ItemTemplate="{DynamicResource DataTemplate_Level1}" FontWeight="Bold" Height="2045" Width="790"
IsSynchronizedWithCurrentItem="True">
</ListBox>
</ScrollViewer>
</Grid> |
La listbox est rempli à partir d'une List<string> lsts:
Code:
1 2
|
lst.ItemsSource = lsts; |
Je cherche ensuite à accèder au ième élément de la list box afin de pouvoir changer le background du bouton:
Code:
1 2
|
lst.SelectedItem = lst.Items.IndexOf("chaine recherchée"); |
Mais comment atteindre le bouton correspondant?
D'avance merci.