[英]How can I create a list with button click and then loop through them with C# wpf?
I have a C# WPF application that has a form with two fields. 我有一个C#WPF应用程序,它有一个包含两个字段的表单。 Every time the form is submitted, I want to get the values and use the Instructor class to add the new item to a list. 每次提交表单时,我都想获取值并使用Instructor类将新项目添加到列表中。 Then, I want to loop through the list and display the items in the ListView element. 然后,我想遍历列表并显示ListView元素中的项目。 I realize I can do this without the class, but having the class is a requirement for my school assignment. 我意识到我可以在没有上课的情况下做到这一点,但是上课是我学校作业的要求。
Here is my Main Window class: 这是我的主窗口类:
public partial class MainWindow : Window
{
private List<Instructor> instList;
public MainWindow()
{
InitializeComponent();
List<Instructor> instList = new List<Instructor> { };
}
private void btnCreateInstructor_Click(object sender, RoutedEventArgs e)
{
spCreateInstructor.Visibility = (spCreateInstructor.Visibility == Visibility.Hidden) ? Visibility.Visible : Visibility.Hidden;
}
private void btnInstructorSubmit_Click(object sender, RoutedEventArgs e)
{
instList.Add(new Instructor { firstName = txtInstructorFirstName.Text, lastName = txtInstructorLastName.Text });
foreach (var inst in instList)
{
lvInstructorList.Items.Add("{0} {1}", inst.firstName, inst.lastName);
//Error occurs on the line above.
}
}
}
This is the instructor class: 这是教师班:
class Instructor
{
public string firstName { set; get; }
public string lastName { set; get; }
}
My problem is that I get an error saying No overload for method Add takes 3 arguments
What am I doing wrong? 我的问题是我得到一个错误说No overload for method Add takes 3 arguments
我做错了什么? I have indicated where the error occurs with a comment in the code. 我已经指出代码中的注释发生错误的位置。
Try replacing this line: 尝试替换此行:
lvInstructorList.Items.Add("{0} {1}", inst.firstName, inst.lastName);
with this one 用这个
lvInstructorList.Items.Add(new Instructor { firstName = inst.firstName, lastName = inst.lastName });
It is similar to how you added to the instList
. 它类似于您添加到instList
。
Edit 编辑
After realizing lvInstructorList is a ListView xaml element, this should work: 在实现lvInstructorList是一个ListView xaml元素后,这应该工作:
var listViewItem = new ListViewItem(new Instructor { firstName = inst.firstName, lastName = inst.lastName });
lvInstructorList.Items.Add(listViewItem);
ah here is how to create a listview and add items to it in wpf check this question Add Items to Columns in a WPF ListView 啊这里是如何创建一个列表视图并在wpf中添加项目检查此问题将项目添加到WPF ListView中的列
In short 简而言之
list view takes an object so you need to add items like that 列表视图需要一个对象,因此您需要添加这样的项目
lvInstructorList.Items.Add(lvInstructorList.Items.Add(new Instructor { firstName = inst.firstName, lastName = inst.lastName });
and you should bind to it in xaml 你应该在xaml中绑定它
<ListView x:Name="lvInstructorList">
<ListView.View>
<GridView>
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding firstName }"/>
</GridView>
</ListView.View>
Not sure why you want to loop again and again to the lvInstructorList. 不确定为什么要一次又一次地循环到lvInstructorList。 But you have to clear every time. 但你必须每次都清楚。
instList.Add(new Instructor { firstName = txtInstructorFirstName.Text, lastName = txtInstructorLastName.Text });
lvInstructorList.Clear();
foreach (var inst in instList)
{
lvInstructorList.Items.Add(inst);
}
It looks to me like you are not intending on binding the list view to an object, but rather just insert string values into the list view. 在我看来,您并不打算将列表视图绑定到对象,而只是将字符串值插入到列表视图中。 If that is the case: 如果是这种情况:
lvInstructorList.Items.Add(string.Format("{0} {1}", inst.firstName, inst.lastName));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.