I’ve been experimenting with the
PathListBox and wanted to create a better experience for selecting an item in the
ListBox. The standard way to select an item in a
ListBox is by clicking on it, but in some situations you want to select an item by just hovering over the item. I explicitly say some situations, because I wouldn’t want to fire up the discussion that hovering over an item isn’t the same as explicitly clicking on an item to select it. First requirement for me was to have something that didn’t require me any coding when I want to apply it more than once. So for me the idea of writing a
Behavior or
TriggerAction does make sense. Second requirement, should work any any regular
ListBox but also on the
PathListBox. Third, should work with data binding.
Setting up the structure So I started creating a
Behavior that can be associated with any
FrameworkElement and could be applied in the
DataTemplate for example. You have to reference the
System.Windows.Interactivity assembly to start.
public class SelectElementOnHover : Behavior<FrameworkElement>
{
protected override void OnAttached()
{
AssociatedObject.MouseEnter += AssociatedObject_MouseEnter;
}
private void AssociatedObject_MouseEnter(object sender, MouseEventArgs e)
{
...
}
protected override ...