Mouseover to select item in listbox in WPF

user2096837 picture user2096837 · Mar 19, 2013 · Viewed 13.8k times · Source

I am relatively new to WPF, but I would like to know how can I enable a listbox to select an item based on a mouseover event instead of the button click. I would like the item to be selected when the mouse is over the selected item, without having to press click first.

Thank you

Answer

Clemens picture Clemens · Mar 19, 2013

You may write a simple ListBoxItem Style with a Trigger on the IsMouseOver property that sets the IsSelected property:

<ListBox>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="IsSelected" Value="True"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>