WPF Listview Getting row values
我有xaml作为文件如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <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> |
下面是代码背后的文件为:
使用系统. 使用system.collections.generic; linq的使用系统。 文本;使用系统。 使用Windows的系统。 使用system.windows.controls; 使用system.windows.data; 使用system.windows.documents; 使用system.windows.input; 使用system.windows.media; 使用system.windows.media.imaging; 使用system.windows.navigation; 使用system.windows.shapes;
namespace comboboxcheck {
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | 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; } } } |
}
我会想得到,这已经employee收集员工的名字,employee employee ID和指定的。我需要一个代码在点击"事件"按钮,得到的价值和给定的格式。
您的绑定工作正常。只需如下获取ListView的项集合:
1 2 3 4 5 6 7 8 9 10 | 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()); } } |
但是,这里的employeedesignation是一个int,如果您想得到实际的employeedesignation实例,可以手工"查询"如下:
1 2 3 4 5 6 7 8 9 10 11 12 | 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); } } |