2010-08-30 158 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