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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With