Create a click event on ListViewItem programmatically in WPF

I'm making a WPF app, this app has a UserControl, which has a list view.

I tried to create a click event listener but i never got it to work right, and i havend find anything to solve it

I fill this list view items with an object like this:

List<AsesoriaClass> listaAsesorias = phpClass.getListaAsesoriasAsesor(asesor.ID);
            foreach (var asesoria in listaAsesorias)
            {
                AsesoriaTable data = new AsesoriaTable(asesoria.AsesoriaID.ToString(), asesoria.ClienteNombre + " " + asesoria.ClienteApellidos, asesoria.FechaInicio.ToString(), asesoria.FechaFinal.ToString());
                this.ListView.Items.Add(data);
            }

And this is the XAML of the User Control:

<ListView x:Name="ListView">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="ID" DisplayMemberBinding="{Binding Path=Id}" Width="100"/>
                    <GridViewColumn Header="Cliente" DisplayMemberBinding="{Binding Path=Cliente}" Width="300"/>
                    <GridViewColumn Header="Inicio" DisplayMemberBinding="{Binding Path=Inicio}" Width="200"/>
                    <GridViewColumn Header="Final" DisplayMemberBinding="{Binding Path=Final}" Width="200"/>
                </GridView>
            </ListView.View>
        </ListView>

I want to make a click listener so, when i click on an item something happens (to start i just want it to show a mesage box). how do i do this?

Upvotes: 0

Views: 4377

Answers (1)

Hammas
Hammas

Reputation: 1214

You can simply attach an Event Handler on SelectionChanged. Like this

 ListView.SelectionChanged += LstOnSelectionChanged;

Where LstOnSelectionChanged is a method.

private void LstOnSelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
  MessageBox.Show("Anything"); 
}

Upvotes: 2

Related Questions