我在附有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 –