简体   繁体   English

从PreviewMouseLeftButtonDown获取Listview或ListViewItem的索引

[英]Get Index of Listview ot ListViewItem from PreviewMouseLeftButtonDown

So I have ListView with MyData object. 所以我有MyData对象的ListView And once I click the Left mouse key without release (so the item is not selected yet) I want to get my item ( ListViewItem or just the ListViewItem index...) 然后单击释放鼠标左键(因此尚未选择该项目),我想获取我的项目( ListViewItem或只是ListViewItem索引...)

So this is what I have tried: 所以这就是我尝试过的:

ListView listView;

private void listView_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    var obj = e.Source;
    MyData myData = (MyData)(sender as ListView).SelectedItem;
    if (myData!= null)
    {
        // Do my stuff...
    }
}

Unfortunately, because the ListViewItem is not selected yet my myData is always null . 不幸的是,由于尚未选择ListViewItem ,因此myData始终为null

Any suggestions? 有什么建议么?

尝试检查((System.Windows.FrameworkElement)e.OriginalSource).DataContext

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM