2010-08-30 59 views
0

我有如下XAML文件:WPF列表视图获取行值

<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> 

隐藏文件的代码如下:

使用系统; using System.Collections.Generic;使用System.Linq的 ; using System.Text;使用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;

命名空间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; } 
    } 
}  

}

我想获得员工收集其中有员工姓名,员工ID和员工称号。我需要在'获取价值'按钮的点击事件中给出一个代码并给出格式。

回答

2

你的绑定工作正常。只需像下面这样获取ListView的Items集合:

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()); 
    } 
} 

但是,这里的EmployeeDesign是一个int。如果你想获得实际的EmployeeDesignation实例,你可以像这样手动“查询”它:

+0

这很棒。该解决方案工作得很好!这是我正在寻找的。谢谢你krmicpuppet先生! – Sathish 2010-08-31 04:37:13