我有XAML文件如下:
<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; 使用System.Collections.Generic; 使用System.Linq; 使用System.Text; 使用System.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;
命名空间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和员工名称的员工集合。我需要在“获取价值”按钮的点击事件中输入代码并给出格式。
答案 0 :(得分: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实例,可以手动“查询”它,如下所示:
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);
}
}