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 32 33
|
<ListBox x:Name="listBox">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
<EventSetter Event="GotFocus" Handler="ListBoxItem_GotFocus"/>
<Setter Property="Tag" Value="{Binding ElementName=listBox}"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
//quelque part dans ton code...
listBox.Items.Add("item1");
listBox.Items.Add("item2");
//
void ListBoxItem_GotFocus(object sender, RoutedEventArgs e)
{
ListBoxItem item = (ListBoxItem)sender;
ListBox lb = (ListBox)item.Tag;
Color color = Colors.Green;
Thickness thickness = new Thickness(2);
switch ((string)item.Content)
{
case "item2":
color = Colors.Red;
thickness = new Thickness(4);
break;
}
lb.BorderBrush = new SolidColorBrush(color);
lb.BorderThickness = thickness;
} |
Partager