2013-03-31 58 views
0

在典型的listBox上有沒有一種方法來確定哪個listBoxItem當前關注? 我正在談論選擇之前的狀態,更像是鼠標懸停事件。獲取listBoxItem焦點

我試過MouseEnterFocus事件,但問題是,我發現沒有辦法確定哪些項目被稱爲那些事件。

回答

1

您應該使用的SelectedIndex或selectedItem

+0

不起作用。 SelectedIndex仍然爲-1,SelectedItem爲null –

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 - 你需要一個附加屬性爲ORT - 例如,(只是一個快速搜索會得到你很多)......

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到您的視圖模型屬性 - 你會得到作爲「懸停項目'然後你就可以完成剩下的工作 - 甚至可以設置/展示你自己的'HoveredItem'屬性(這就是我應該這麼做的方式 - 把所有的東西連接在一起,而不是直接連接到那個外殼的屬性或者其他東西。這是一個很快的僞代碼,希望它有幫助。