我创建了一个具有员工属性的类。我希望我的程序能够将员工添加到员工列表中,然后再添加ListView
更新。
类文件:
public class Employee
{
public string first { get; set; }
public string last { get; set; }
public int passcode { get; set; }
}
public class employeeManager
{
public static List<Employee> GetEmployee()
{
var employees = new List<Employee>();
employees.Add(new Employee { first = "John", last = "Doe", passcode = 123 });
return employees;
}
}
page.xaml.cs:
Employees.Add(new Employee { first = firstName.Text, last = lastName.Text });
string fullName = Employees[Employees.Count -1].first + " " + Employees[Employees.Count-1].last;
employeeListView.Items.Add(fullName);
Page.xaml:
<Page.Resources>
<DataTemplate x:DataType="data:Employee" x:Key="EmployeeListDataTemplate" >
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
<StackPanel Margin="5,0,0,0" Orientation="Horizontal">
<TextBlock Text="{x:Bind first}" HorizontalAlignment="Left" FontSize="25" />
<TextBlock Text="{x:Bind last}" Margin="5,0,0,0" HorizontalAlignment="Left" FontSize="25" />
</StackPanel>
</StackPanel>
</DataTemplate>
</Page.Resources>
<ListView Margin="0,33,0,0" x:Name="employeeListView" IsItemClickEnabled="True" ScrollViewer.VerticalScrollBarVisibility="Auto" ItemsSource="{x:Bind Employees}" Background="#00F2F2F2" ItemTemplate="{StaticResource EmployeeListDataTemplate}">
</ListView>
page.xaml.cs中的最后一行:给我一个错误:
system.Exception:“灾难性的失败
答案 0 :(得分:0)
如果使用employeeListView.Items.Add(fullName);
,则不应使用Itemsource绑定列表。
您可以尝试使用ObservableCollection而不是ObservableCollection:INotifyCollectionChanged
可以在更改集合时通知的列表。
如果要在ObservableCollection中添加或删除可添加或远程的项目。