WPF Listview Getting row values

listviewwpf

I have XAML File as below:

<Window x:Class="ComboBoxCheck.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:check="clr-namespace:ComboBoxCheck"
Title="Window1" Height="300" Width="320">
<Window.Resources>
    <ObjectDataProvider x:Name="Designation" MethodName="GetDesignations" ObjectType="{x:Type check:Window1}" x:Key="Designation" IsAsynchronous="True"/>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="30"/>
    </Grid.RowDefinitions>
    <ListView Grid.Row="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Name="lsvStaffList"  Margin="0,0,0,3"
          BorderBrush="Transparent" BorderThickness="0">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Employee Id" Width="70" DisplayMemberBinding="{Binding Path=EmployeeId}"/>
                <GridViewColumn Header="Employee Name" Width="90" DisplayMemberBinding="{Binding Path=EmployeeName}"/>
                <GridViewColumn Header="Designation" Width="120">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox Name="cmbDesignation" Height="20" Width="90" SelectedValue="{Binding Path=EmployeeDesignation}"
                                      ItemsSource="{Binding Source={StaticResource Designation}}"
                                      DisplayMemberPath="Name" SelectedValuePath="Id"
                                      VerticalAlignment="Top" HorizontalAlignment="Stretch"/>        
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>                    
            </GridView>
        </ListView.View>
    </ListView>
    <Button Name="btnProperty" Width="75" Content="Get Value" Height="25" Click="btnProperty_Click" Grid.Row="1"/>
</Grid>

The code behind file is as below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ComboBoxCheck
{

public partial class Window1 : Window
{
    public static Designations designations = null;
    public Employees employees = null;

    public Window1()
    {
        InitializeComponent();

        designations = new Designations();
        employees = new Employees();

        Designation d1 = new Designation();
        d1.Id = 1;
        d1.Name = "Manager";
        designations.Add(d1);

        Designation d2 = new Designation();
        d2.Id = 2;
        d2.Name = "Developer";
        designations.Add(d2);

        Designation d3 = new Designation();
        d3.Id = 3;
        d3.Name = "Lead";
        designations.Add(d3);

        Employee e1 = new Employee();
        e1.EmployeeId = 1;
        e1.EmployeeName = "Name1";
        e1.EmployeeDesignation = 2;
        employees.Add(e1);

        Employee e2 = new Employee();
        e2.EmployeeId = 2;
        e2.EmployeeName = "Name2";
        e2.EmployeeDesignation = 2;
        employees.Add(e2);

        Employee e3 = new Employee();
        e3.EmployeeId = 3;
        e3.EmployeeName = "Name3";
        e3.EmployeeDesignation = 1;
        employees.Add(e3);

        lsvStaffList.ItemsSource = employees;

    }

    public static Designations GetDesignations()
    {
        return designations;
    }

    private void btnProperty_Click(object sender, RoutedEventArgs e)
    {
        //I need something like this
        //Employees employeesCollection = new Employees();
        //employeesCollection[0].EmployeeId = 1
        //employeesCollection[0].EmployeeName = Name1
        //employeesCollection[0].EmployeeDesignation = Developer

        //employeesCollection[1].EmployeeId = 2
        //employeesCollection[1].EmployeeName = Name2
        //employeesCollection[1].EmployeeDesignation = Developer

        //employeesCollection[2].EmployeeId = 3
        //employeesCollection[2].EmployeeName = Name3
        //employeesCollection[2].EmployeeDesignation = Manager
    }

}

public class Designations : List<Designation> {}

public class Designation
{
    private int id;

    public int Id
    {
        get { return id; }
        set { id = value; }
    }
    private string name;

    public string Name
    {
        get { return name; }
        set { name = value; }
    }

}

public class Employees : List<Employee> { }

public class Employee
{
    private int employeeid;

    public int EmployeeId
    {
        get { return employeeid; }
        set { employeeid = value; }
    }
    private string employeename;

    public string EmployeeName
    {
        get { return employeename; }
        set { employeename = value; }
    }
    private int employeedesignation;

    public int EmployeeDesignation
    {
        get { return employeedesignation; }
        set { employeedesignation = value; }
    }
}    

}

I would like to get the employees collection which has employee name, employee id and employee designation. I need a code in the click event of 'Get Value' button and given the format.

Best Answer

Your bindings work fine. Just get the Items collection of your ListView like this:

private void btnProperty_Click(object sender, RoutedEventArgs e)
{
    IEnumerable items = this.lsvStaffList.Items;
    foreach (Employee employee in items)
    {
        Console.WriteLine(employee.EmployeeId.ToString() 
            + "," + employee.EmployeeName.ToString() 
            + "," + employee.EmployeeDesignation.ToString());
    }
}

However, the EmployeeDesignation here is an int. If you want to get the actual EmployeeDesignation instance, you can manually "query" it like this:

private void btnProperty_Click(object sender, RoutedEventArgs e)
{
    IEnumerable items = this.lsvStaffList.Items;
    foreach (Employee employee in items)
    {
        Designation d = designations.First(p => p.Id == employee.EmployeeDesignation);

        Console.WriteLine(employee.EmployeeId.ToString() 
            + "," + employee.EmployeeName.ToString() 
            + "," + d.Name);
    }
}