private static void ListBox_MouseLeave(object sender, MouseEventArgs e) { ListBox lb = sender as ListBox; // Remove any previous Adorners AdornerInfo prevInfo = lb.GetValue(AttachedAdornerProperty) as AdornerInfo; AdornerLayer layer = AdornerLayer.GetAdornerLayer(lb); if (prevInfo != null) { if (layer != null) { layer.Remove(prevInfo.Adorner); lb.ClearValue(AttachedAdornerProperty); } } }
private static void ListBox_MouseMove(object sender, MouseEventArgs e) { // Check that we are hovering on a ListBoxItem ListBox lb = sender as ListBox; ListBoxItem item = lb.ContainerFromElement(e.OriginalSource as Visual) as ListBoxItem; if (item == null) { return; } // Remove any previous Adorners AdornerInfo prevInfo = lb.GetValue(AttachedAdornerProperty) as AdornerInfo; AdornerLayer layer = AdornerLayer.GetAdornerLayer(lb); if (prevInfo != null) { if (prevInfo.ListItem == item) { return; } layer.Remove(prevInfo.Adorner); lb.ClearValue(AttachedAdornerProperty); } // Attach new adorner to current ListBoxItem HoverAdorner adorner = new HoverAdorner(item); adorner.Container.Content = lb.ItemContainerGenerator.ItemFromContainer(item); adorner.Container.ContentTemplate = item.FindResource("AdornerTemplate") as DataTemplate; layer.Add(adorner); AdornerInfo info = new AdornerInfo(); info.Adorner = adorner; info.ListItem = item; lb.SetValue(AttachedAdornerProperty, info); }