WPF ListView - detect when selected item is clicked

前端 未结 7 942
刺人心
刺人心 2020-11-29 02:10

I\'m using a WPF ListView control which displays a list of databound items.


    
                 


        
7条回答
  •  隐瞒了意图╮
    2020-11-29 02:34

    Use the ListView.ItemContainerStyle property to give your ListViewItems an EventSetter that will handle the PreviewMouseLeftButtonDown event. Then, in the handler, check to see if the item that was clicked is selected.

    XAML:

    
        
            
                
            
        
        
            
        
    
    

    Code-behind:

    private void ListViewItem_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        var item = sender as ListViewItem;
        if (item != null && item.IsSelected)
        {
            //Do your stuff
        }
    }
    

提交回复
热议问题