如何實現下面的GetListBoxItemIndex
函數來獲取我單擊的項目的索引?我嘗試使用VisualTreeHelper
沒有成功(意思是,VisualTreeHelper
明顯的作品,但我不與樹搜索Anywhere入門...)如何獲得沒有SelectedIndex之類的ListBox項目的索引(在PreviewMouseDown中,沒有任何「已選擇」)
private void MyListBox_OnPreviewMouseDown(object sender, MouseButtonEventArgs e){
var listBox = sender as ListBox;
var src = e.OriginalSource as DependencyObject;
if (src == null || listBox == null) return;
var i = GetListBoxItemIndex(listBox,src);
DragDrop.DoDragDrop(src, BoundCollection[i], DragDropEffects.Copy);
// BoundCollection defined as:
// ObservableCollection<SomeDataModelType> BoundCollection
}
請注意,沒有什麼在這種狀態下還選擇了,因爲它是一個PreviewMouseDown
事件
謝謝,它的工作原理。但你能解釋爲什麼我需要'ItemContainerGenerator'嗎?爲什麼我不能「樹狀漫步」('VisualTreeHelper'方法)來獲取物品? – Tar
請看看更新。 – abdelkarim