我在附有MouseMove
事件處理程序的WPF應用程序中有ListBox
。我想要做的就是使用這個事件來獲取鼠標所在物品的索引。我的代碼ListBox上的ListBoxItem索引mouseover
簡單的例子:
<StackPanel>
<ListBox x:Name="MyList" MouseMove="OnMouseMove"/>
<Separator/>
<Button>Beep</Button>
</StackPanel>
public CodeBehindConstructor()
{
List<string> list = new List<string>();
list.Add("Hello");
list.Add("World");
list.Add("World"); //Added because my data does have duplicates like this
MyList.ItemsSource = list;
}
public void OnMouseMove(object sender, MouseEventArgs e)
{
//Code to find the item the mouse is over
}
工程非常好。由於OP使用'ListBox'而不是'ListView',所以在你的例子中'ListViewItem'的所有實例都應該改爲'ListBoxItem'。 – Stewbob
我的不好,我編輯爲使用列表框 – Mathieu
這似乎工作。我在VisualTreeHelper的周圍掙扎着,但這讓我正確地修復了它。 Ta:D –