Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
3.75/5 (4 votes)
See more:
Hello,

I have a ComboBox in WPF ,
I need to get the index of highlighted Item
Not Selected Item and not Selected Index

for example:

int a = MyComboBox.SelectedIndex;


but I don't need this, I need the Index of Highlighted Item.

I mean I want the Index of the Item which I am scrolling on that with keyboard (Up , Down) and Mouse.

Thank you.
Posted
Updated 15-Mar-11 1:59am
v3
Comments
Sergey Alexandrovich Kryukov 15-Mar-11 20:42pm    
Excellent question, my 5. I could not find the answer! Wow! I used to do it for Forms only.
--SA
Prerak Patel 15-Mar-11 23:39pm    
I think, now my updated answer may help you have a look at how it works.
Sergey Alexandrovich Kryukov 16-Mar-11 0:39am    
I think you got it! I'm testing...
--SA
MHMDD 16-Mar-11 1:48am    
Thank you SAKryukov.
I don't realy know, Why microsoft hasn't presented any event for it ?!
Sergey Alexandrovich Kryukov 16-Mar-11 2:55am    
Say thanks to Prerak. You know, there are good amount of functionality holes like that. There are two events, they are not fired (probably fired in other classes derived from Selector, not this one). Do you know that combo box is notoriously problematic. Internally, it consists of two windows: edit and list. It's very typical for libraries to provide separate access to each. Many other components have problem: TreeView, ListView. There is no virtual mode for Tree Views -- very wanted...
--SA

Add code to MouseMove of ComboBoxItem

or

<ComboBoxItem MouseMove="ComboBoxItem_MouseMove">Item 1</ComboBoxItem>


VB
Private Sub ComboBoxItem_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Input.MouseEventArgs)
 'ComboBox1.Items.IndexOf(DirectCast(sender, ComboBoxItem)
End Sub

This is how you can get index of that item.

[Edit]
You can use this to get highlighted item on MouseMove and KeyDown of ComboBox.

VB
Private Function GetHighlightedItem(ByVal cbox As ComboBox) As Integer
  For Each item As ComboBoxItem In cbox.Items
    If item.IsHighlighted Then
      Return ComboBox1.Items.IndexOf(item)
    End If
  Next
  Return -1
End Function
 
Share this answer
 
v4
Comments
MHMDD 15-Mar-11 7:54am    
Thank you for answering
But:

It doesn't Work. I want to get the index of my highlighted Item, when I am scrolling on Items.

with Mouse and keyboard.
Prerak Patel 15-Mar-11 23:38pm    
Hey, you should wait before vote the answer down. I was off. Anyways, updated the answer. Check it if it helps.
MHMDD 16-Mar-11 1:02am    
I didn't vote down. I didn't vote at all.
Prerak Patel 16-Mar-11 1:03am    
I thought that it is voted down just because I couldn't update it in time, so asked you to wait. Anyways, did it help?
Venkatesh Mookkan 16-Mar-11 0:35am    
Dirty way. But definite would work. Have 5!
Hopefully, this thread[^] will give you a solution.
 
Share this answer
 
Comments
Venkatesh Mookkan 16-Mar-11 0:37am    
Good reference.
Abhinav S 16-Mar-11 0:46am    
Thanks.
Tarun.K.S 16-Mar-11 2:18am    
A better method. My 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900