Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF listbox select item on mouse over

Tags:

.net

wpf

xaml

I'm trying to make a style for a listbox which will set the selected item to an item when the item has the mouse on it.

Any hints?

like image 612
Chris McGrath Avatar asked Apr 11 '10 09:04

Chris McGrath


1 Answers

You can do it using a style in the ListBox itself that affects all its items:

<ListBox.Resources>
    <Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsMouseOver,RelativeSource={RelativeSource Self}}" 
                         Value="True">
                <Setter Property="IsSelected" Value="True" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</ListBox.Resources>

That'll set the IsSelected property on the item to true when the IsMouseOver property is true. Provided your ListBox isn't multi-select it works the way you'd expect.

like image 63
Matt Hamilton Avatar answered Sep 23 '22 18:09

Matt Hamilton