简体   繁体   English

如何创建一个按钮单击列表,然后使用C#wpf循环它们?

[英]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.

相关问题 在循环C#WPF中单击事件按钮 - Click event for button in loop C# WPF 如何通过 C# 代码(不是 XAML)旋转在 WPF 中单击按钮时动画的图像 - How do i rotate an image animated on button click in WPF through C# code (Not XAML) C#-如何创建具有不同控件的2个单独的按钮数组,而又不引起其他控件的问题? - C# - How can I create 2 separate button arrays with different controls without them causing problems with the other? 我如何在c#(silverlight)中创建动态类,在其中可以遍历属性 - how can i create a dynamic class in c#(silverlight) where i can loop through properties 当我在 C# WPF 中使用 for 循环创建按钮时,为什么它们没有显示在网格中? - Why dont buttons show up in the grid when I create them using a for loop in C# WPF? c# 循环遍历列表视图项,然后单击按钮在文本框中一次显示一行 - c# loop through list view items and display each in a text box a row at a time with a click of a button 如何动态创建文件并将其提供给用户(单击下载按钮)? C# - How can I dynamically create and serve a file to a user (on click of a download button)? C# C#WPF按钮单击 - C# WPF button click 如何在 XAML 和 C# 中为 WPF 按钮的旋转设置动画? - How can I animate the rotation of a WPF Button in XAML AND in C#? 如何更改WPF C#中的默认按钮突出显示 - How can I change the default button highlight in WPF C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM