鼠标悬停时WPF列表框选择项目

时间:2010-04-11 09:21:29

标签: .net wpf xaml

我正在尝试为列表框创建一个样式,当项目上有鼠标时,它会将所选项目设置为项目。

任何提示?

1 个答案:

答案 0 :(得分:13)

您可以使用ListBox本身中影响其所有项目的样式来执行此操作:

<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>

当IsMouseOver属性为true时,它会将项目上的IsSelected属性设置为true。如果您的ListBox不是多选的,它会按您期望的方式工作。

相关问题