Project requirements, Listbox selects pictures of the IMAGE control that the selected item, and the corresponding image control of each other listboxitem is set to the unscotained picture.
solution is suitable for ComboBox, etc.:
1. Look at the xaml file first:
<DataTemplate x:Name="BusTemplate" x:Key="LBDataTemplate">
<StackPanel>
<StackPanel Orientation="Horizontal" Margin="10">
<TextBlock Text="{Binding BusStr}" VerticalAlignment="Center"
FontSize="30" Foreground="White" Width="360"/>
<Image x:Name="BusStausImage" Source="/Image/d1.png"/>
</StackPanel>
</StackPanel>
</DataTemplate>
<ListBox ItemsSource="{Binding}" x:Name="BusList" SelectionChanged="ListBox_SelectionChanged"
ItemTemplate="{StaticResource LBDataTemplate}">
2. Now get the control set in all dynamic binding items of the Lisxbox above, and then replace it with the default picture. Then replace the current selected item with the picture of the selected style
<Image x:Name="BusStausImage" Source="/Image/1.png"/>
3. SELECTIONCHANGED Event of Listbox
Private void listbox_selectionChanged
{{
try
{{
// Through the functionFindFirstVisualchild traversed Lisxbox to get the Image collectionList <IMAGE> Imglist = FindFirstVisualChild <IMAGE> (BusList, "BusStausImage");
Foreach (Image Img in Imglist)
{{
img.source = new bitmapimage (new uri ("/image/vehicle /d1.png", urikind.relative);
}
// Get the current selection itemiMage,FindfirstVisualchild2 to get a single
ListBoxItem myListBoxItem = (ListBoxItem)BusList.ItemContainerGenerator.ContainerFromItem(BusList.SelectedItem);
Image image = FindFirstVisualChild2<Image>(myListBoxItem, "BusStausImage");
image.Source = new BitmapImage(new Uri("/Image/d2.png", UriKind.Relative));
}
catch (Exception)
{
}
}
Custom method 1: FindFirstVisualchild:
// Traversing the designated container, returned to the adapted control set according to the control name
Public list <t> FindFirsualchild <T> (DependencyObject Obj, String Childname) where T: DependencyObject
{{
List <t> Childlist = New list <t> ();
// Traversing the specified container
for (int i = 0; I <visualtreehelper.getchildrenCount (obj); i ++)
{{
DependencyObject Child = VisualTreeheelper.getchild (obj, i);
If (Child! = Null && Child is t && Child.getValue (nameProperty) .tring () == Childname)
{{
childlist.add ((t) Child);
}
// If it does not exist, continue to traverse the sub -container of the specified container (if the sub -container of the specified container is the second layer, the control of the matching control is jumped out. layer)
else
{{
List <t> T2 = FindFirstVisualChild <T> (Child, Childname);
if (T2! = Null && T2.Count> 0)
{{
FOREACH (T T in T2)
{{
childlist.add (t);
}
}
}
}
Return Childlist;
}
Custom method 2: FindFirstVisualChild2
// Like the container, return the first adapted control according to the control name
public T FindFirsualchild2 <T> (DependencyObject Obj, String Childname) where T: DependencyObject
{{
for (int i = 0; I <visualtreehelper.getchildrenCount (obj); i ++)
{{
DependencyObject Child = VisualTreeheelper.getchild (obj, i);
If (Child! = Null && Child is t && Child.getValue (nameProperty) .tring () == Childname)
{{
Return (t) child;
}
else
{{
T childofchild = FindFirsualchild2 <T> (Child, Childname);
If (Childofchild! = Null)
{{
Return Childofchild;
}
}
}
Return null;
}
custom method 3:
// Elves all sub -controls (including sub -controls of the child control) of the specified container, and return to the control set according to the specified control name. If "" is passed, it returns the collection of all sub -spaces
public list <t> getChildObjects <T> (DependencyObject Obj, String name) where t: frameworklement
{{
DependencyObject Child = null;
List <t> Childlist = New list <t> ();
For (int i = 0; I <= visualtreehelper.getchildrenCount (obj) - 1; i ++)
{{
child = visualtreehelper.getchild (obj, i);
If (Child is t && ((t) Child) .name == name || String.isnullorempty (name))
{{
childlist.add ((t) Child);
}
Childlist.adDRange (getChildObjects <t> (child, ""); //
}
Return Childlist;
}
References: