How to tell which column of a ListView was clicked

axnst2
axnst2 used Ask the Experts™
on
Hi Experts,

     I have a ListView in a WPF Window.  I need to do certain things based on the particular column of a row that the user picks on.  Basically if the user clicks on the Info icon in that column I want to not select the row, but rather just pop-up a window and display information specific to the item that is stored in that row.  If the user clicks on any other column of the row, then I want to select the row and trigger a whole different set of code.  How can I do that?  I can retrieve the selected row, but I have no way to know which column was clicked. Or do I?

Thanks!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2012
Top Expert 2008
Commented:
I don't know if this is the kind of example that you need, but it shows how to configure a ListViewItem for a popup.

Popup Class
http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.popup.aspx#1

<Window x:Class="LdiaryPopupToolTip.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="How To Use a Popup Window as a ListViewItem ToolTip (Sample created by Ldiary)" Height="200" Width="250">    
 <StackPanel Name="motherPanel">        
   <Popup  Height="Auto" Width="Auto" Name="listViewItemToolTip" StaysOpen="True" AllowsTransparency="True"  >            
    <Border BorderThickness="2" Background="ForestGreen" >                
     <StackPanel Margin="20"  Orientation="Horizontal">                    
      <TextBlock Text="Age is "/>                    
      <TextBlock  Name="popupTextBlock" />                    
      <TextBlock Text=" yrs old."/>                
     </StackPanel>            
    </Border>        
   </Popup>        
 <ListView ItemsSource="{Binding}">            
  <ListView.ItemContainerStyle>                
   <Style TargetType="{x:Type ListViewItem}">                    
    <EventSetter Event="Mouse.MouseEnter" Handler="Get_Ldiary_PopupToolTip" />                    
    <EventSetter Event="Mouse.MouseLeave" Handler="Remove_Ldiary_PopupToolTip"/>                
   </Style>            
</ListView.ItemContainerStyle>            
  <ListView.View>                
   <GridView >                    
    <GridView.Columns>                        
     <GridViewColumn Header="First Name" DisplayMemberBinding="{Binding Path=First}" />                        
     <GridViewColumn Header="Last Name" DisplayMemberBinding="{Binding Path=Last}" />                    
    </GridView.Columns>                
   </GridView>            
  </ListView.View>        
 </ListView>    
 </StackPanel>
</Window>

Open in new window


using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using System.Collections.Generic;
using System.Windows.Controls.Primitives;
 
namespace LdiaryPopupToolTip
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            List<Person> lst = new List<Person>();
            Person ps = new Person();
            ps.First = "Ldiary";
            ps.Last = "Translations";
            ps.Age = "21";
            lst.Add(ps);
            ps = new Person();
            ps.First = "L";
            ps.Last = "English";
            ps.Age = "30";
            lst.Add(ps);
            ps = new Person();
            ps.First = "Diary";
            ps.Last = "Japanese";
            ps.Age = "19";
            lst.Add(ps);
            motherPanel.DataContext = lst;
        }
 
        private void Get_Ldiary_PopupToolTip(object sender, MouseEventArgs e)
        {
            ListViewItem listViewItem = e.Source as ListViewItem;
            Person person = listViewItem.Content as Person;
            popupTextBlock.Text = person.Age;
            listViewItemToolTip.PlacementTarget = listViewItem;
            listViewItemToolTip.Placement = PlacementMode.MousePoint;
            listViewItemToolTip.IsOpen = true;
        }
        private void Remove_Ldiary_PopupToolTip(object sender, MouseEventArgs e)
        {
            listViewItemToolTip.IsOpen = false;
        }
 
    }
 
    public class Person
    {
        private string _first;
        public string First
        {
            get { return _first; }
            set { _first = value; }
        }
        private string _last;
        public string Last
        {
            get { return _last; }
            set { _last = value; }
        }
        private string _age;
        public string Age
        {
            get { return _age; }
            set { _age = value; }
        }
    }
}

Open in new window

axnst2IT Manager

Author

Commented:
That is not what I was looking for and to be honest after researching it I don't think that there is a way to do this...
axnst2IT Manager

Author

Commented:
I basically have a column that shows an image of a question mark.  When the user clicks the image I want to display certain things about the record.  If the user clicks on the record under any other column header, I am supposed to process the record (In this case update its business state and move it to the end of the list)  So I need to know which column was clicked on and I don't think there is a way to know this under WPF.  Unless I am wrong...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial