开发者

WPF listbox select item on mouse over

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜