Search code examples
c#wpf

C# WPF Pass selected ListView item info on button click?


I would like to preface this with the fact that I'm only just starting to learn WPF.

I created a small window in my learning project and right now I'm trying to figure out the best way to pass a selected object information from the ListView to an event that happens on button click (in this case, Remove).

The window looks like this: window Xaml for it is this: `

<Grid>
    <Button Content="Add" HorizontalAlignment="Right" Margin="0,10,10,0" VerticalAlignment="Top" Width="50" Height="25" Click="Button_Click_Add"/>
    <Button Content="Edit" HorizontalAlignment="Right" Margin="0,40,10,0" VerticalAlignment="Top" Width="50" Height="25" Click="Button_Click_Edit"/>
    <Button Content="Remove" HorizontalAlignment="Right" Margin="0,100,10,0" VerticalAlignment="Top" Width="50" Height="25" Click="Button_Click_Remove"/>
    <Button Content="Refresh" HorizontalAlignment="Right" Margin="0,70,10,0" VerticalAlignment="Top" Width="50" Height="25" Click="Button_Click_Refresh"/>
    <ListView Name="lvEmployeeList" Margin="10,10,65,10">
        <ListView.View>
            <GridView>
                <GridViewColumn Width="150" Header="First Name" DisplayMemberBinding="{Binding FirstName}" />
                <GridViewColumn Width="150" Header="Last Name" DisplayMemberBinding="{Binding LastName}"/>
                <GridViewColumn Width="150" Header="Position" DisplayMemberBinding="{Binding Position}"/>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

`

And the relevant info for MainWindow class: `

    public MainWindow()
    {
        InitializeComponent();

        EmployeeList employeeList = EmployeeList.Load();
        lvEmployeeList.ItemsSource = employeeList.employeeRecord;

    }

    private void Button_Click_Add(object sender, RoutedEventArgs e)
    {
        var newWindow = new UserInfo();
        newWindow.Show();
    }
    private void Button_Click_Edit(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("You have chosen to Edit");
    }
    private void Button_Click_Remove(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("Removing");
    }
    private void Button_Click_Refresh(object sender, RoutedEventArgs e)
    {
        EmployeeList employeeList = EmployeeList.Load();
        lvEmployeeList.ItemsSource = employeeList.employeeRecord;
        MessageBox.Show("Attempting a Refresh");
    }
}`

A lot of it has been placeholder functions, so I could eventually get to actually deal with the data. Each object does have a First Name, Last Name, Position, and a hidden ID behind it though. My plan was to somehow get the object ID and use that for the remove function, but I'm unsure at how to get it from a selected object in the ListView, and then use it only on button click event for the specific button clicked.


Solution

  • I assume you have an Employee class like:

    public class Employee
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Position { get; set; }
    }
    

    Then you can use the following code to get the Employee Id:

    private void Button_Click_Remove(object sender, RoutedEventArgs e)
    {
        Employee selectedEmployee = (Employee)lvEmployeeList.SelectedItem;
        int employeeId = selectedEmployee.Id;
    }