WPF ListView: Attaching a double-click (on an item) event

前端 未结 7 1384
陌清茗
陌清茗 2020-11-29 19:12

I have the following ListView:


    
        
            

        
7条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-29 19:49

    My solution was based on @epox_sub's answer which you should look at for where to put the Event Handler in the XAML. The code-behind didn't work for me because my ListViewItems are complex objects. @sipwiz's answer was a great hint for where to look...

    void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        var item = ListView.SelectedItem as Track;
        if (item != null)
        {
          MessageBox.Show(item + " Double Click handled!");
        }
    }
    

    The bonus with this is you get the SelectedItem's DataContext binding (Track in this case). Selected Item works because the first click of the double-click selects it.

提交回复
热议问题