获取listBoxItem的焦点

时间:2013-03-31 16:52:07

标签: c# xaml windows-phone-8

在典型的listBox上有没有办法确定当前关注哪个listBoxItem? 我在谈论选择之前的状态,更像是鼠标悬停事件。

我尝试了MouseEnterFocus事件,但问题是,我发现无法确定哪个项目称为这些事件。

2 个答案:

答案 0 :(得分:1)

您应该使用SelectedIndex或SelectedItem

答案 1 :(得分:0)

你需要更精细的东西......

请参阅此内容,了解基于'解决方案 - 虽然I wouldn't recommend that (unless a fast fix or very specific) as it 'ties' your code, you should keep MVVM as much as possible ...

ListBox Mouse Over - How to get the item

<ListBox.ItemContainerStyle> 
    <Style TargetType="{x:Type ListBoxItem}">  
        <EventSetter Event="MouseEnter" Handler="ListBoxItem_MouseEnter" /> 
    </Style> 
</ListBox.ItemContainerStyle> 

要解决这个问题与MVVM(i.e. you need to bind that event as a command into your view-model ICommand - 一起工作 - 你需要一个附加的属性 - 例如(只是一个快速搜索会让你很多)......

How can I tie a WPF Command to a MouseEnter event?
How can I execute a command binding on MouseEnter of a StackPanel in WPF?
WPF – Execute button command on MouseOver

然后将attached property绑定到您的视图模型属性 - 您将获得&#39; hovered item&#39;变化 - 然后你可以做其余的事 - 甚至设置/展示你自己的HoveredItem&#39;属性(它是我如何做到的 - 将所有连接在一起,而不是直接命令连接到那个笨拙的属性或其他东西。这是一个伪代码,希望它有所帮助。