开发者

WPF listbox select item on mouse over

开发者 https://www.devze.com 2022-12-27 00:36 出处:网络
I\'m trying to make a style for a listbox which will set the selected item to an item w开发者_如何学Gohen the item has the mouse on it.

I'm trying to make a style for a listbox which will set the selected item to an item w开发者_如何学Gohen 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消