0

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?

6
  • Please define, "never got it right" or better yet, place the code so that it's possible to help you with fixing the code. Also please clarify, do you want to create the event handler at run time or do you want to define it in XAML? Commented May 25, 2019 at 19:03
  • Sorry. As you can see on the code, i create the items programmatically, so, i want to asign an on click event on those items and has to be done programmatically, and it has to have an index, p.e.: if i click on the second item, the index of the event will be 1 and so on. I hope i explained myself correctly. Feel free to ask any other question. Commented May 25, 2019 at 19:21
  • 3
    It's a ListView. Just bind its SelectedItem or SelectedIndex property. Alternatively, attach a handler to its SelectionChanged event. Commented May 25, 2019 at 19:42
  • 1
    I'd recommend using data binding to add the items to the ListView and also for determine which item is selected as @Clemens suggested in his comment which I just upvoted. WPF allows you to to a lot of work declaratively as opposed to writing out the logic in event handlers. Commented May 25, 2019 at 19:46
  • Instead of programmatically adding elements to the ListView's Items collection, you would usually bind its ItemsSource property to a collection-type property of a view model class (google MVVM). The view model would also have a property for the selected item, to which the ListView's SelectedItem would be bound. Commented May 25, 2019 at 19:47

1 Answer 1

2

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"); 
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.