Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何遍历列表框中的所有项_C#_Wpf_Listbox - Fatal编程技术网

C# 如何遍历列表框中的所有项

C# 如何遍历列表框中的所有项,c#,wpf,listbox,C#,Wpf,Listbox,我有一个列表框,其中有一个ObservableCollection的ItemSource 假设我在列表框中添加了2项,那么ObservableCollection包含2个字符串,对吗 现在。。我正在尝试创建一个messagebox弹出窗口,其中包含字符串中的内容。 假设我有一个包含两个项目的列表框,Cat和Bob 现在,当我按下I按钮时,我希望它用两个消息框提示我,一个是Cat,一个是bob。 我试着做一个foreach语句,但当我按下按钮时它不想执行。 现在。。ObservableCollec

我有一个列表框,其中有一个ObservableCollection的ItemSource 假设我在列表框中添加了2项,那么ObservableCollection包含2个字符串,对吗

现在。。我正在尝试创建一个messagebox弹出窗口,其中包含字符串中的内容。 假设我有一个包含两个项目的列表框,Cat和Bob

现在,当我按下I按钮时,我希望它用两个消息框提示我,一个是Cat,一个是bob。 我试着做一个foreach语句,但当我按下按钮时它不想执行。 现在。。ObservableCollection在1个窗口中,我按下的按钮在另一个窗口中,所以我不确定这是否有区别

我所做的是,我这样声明了这个窗口顶部的另一个窗口

    People peps = new People();


    foreach(string email in peps.recipients)
    {
        if (comboBox1.Text == "Email")
        {

            MessageBox.Show(peps.Listbox1.Items.ToString());
            MessageBox.Show(peps.Listbox1.Items.ToString());
        }
    }
但它没有打印出任何东西


我如何让MessageBox显示Listbox1中的内容,而不实际看到您的
人员
类或许多其他东西,并对您试图完成的任务进行鲁莽的假设,我将假设您的
人员
类如下所示:

class People
{
    public string m_sName {get; set; }
    public string m_sEmail {get; set; }

    public People(){}
    public People(string name = null, string email = null)
    {
        m_sName = name;
        m_sEmail = email;
    }
然后,在填写完
列表框后,您希望浏览内容,并将其显示在
消息框中

List<people> lstPeeps = new List<people>();

foreach (var item in ListBox1.Items)
{
   if (item.Contains("@"))    //This checks to see if it's an email address...                         
       lstPeeps.Add(item as peep.m_sEmail);
   else lstPeeps.Add(item as peep.m_sName);
foreach (var peep in lstPeeps)
    MessageBox.Show(peep.name + "\t" + peep.email);
List lspeeps=new List();
foreach(ListBox1.Items中的变量项)
{
if(item.Contains(“@”)//检查是否是电子邮件地址。。。
lspeeps.Add(作为peep.m_sEmail的项目);
else lspeeps.Add(作为peep.m_sName的项目);
foreach(lstPeeps中的变量peep)
MessageBox.Show(peep.name+“\t”+peep.email);

也许是这样的……如果这不起作用,给我们多一点时间,我会相应地修改答案。

在没有看到你的
班级或其他很多事情的情况下,不顾后果地假设你正在努力完成的任务,我会假设你的
是s看起来像这样:

class People
{
    public string m_sName {get; set; }
    public string m_sEmail {get; set; }

    public People(){}
    public People(string name = null, string email = null)
    {
        m_sName = name;
        m_sEmail = email;
    }
然后,在填写完
列表框后,您希望浏览内容,并将其显示在
消息框中

List<people> lstPeeps = new List<people>();

foreach (var item in ListBox1.Items)
{
   if (item.Contains("@"))    //This checks to see if it's an email address...                         
       lstPeeps.Add(item as peep.m_sEmail);
   else lstPeeps.Add(item as peep.m_sName);
foreach (var peep in lstPeeps)
    MessageBox.Show(peep.name + "\t" + peep.email);
List lspeeps=new List();
foreach(ListBox1.Items中的变量项)
{
if(item.Contains(“@”)//检查是否是电子邮件地址。。。
lspeeps.Add(作为peep.m_sEmail的项目);
else lspeeps.Add(作为peep.m_sName的项目);
foreach(lstPeeps中的变量peep)
MessageBox.Show(peep.name+“\t”+peep.email);

可能是这样的……如果这不起作用,请给我们更多的信息,我会相应地修改答案。

这是完整的代码还是您遗漏了什么?何时加载peps.recipients?“假设我在列表框中添加了2项,那么ObservableCollection包含2个字符串,对吗?”不,请不要这样说,甚至不要这样想。要添加项目,请将它们添加到ObservableCollection。迭代ObservableCollection中的项目。如果要删除项目,请将它们从ObservableCollection中删除。列表框只向用户显示ObservableCollection中的内容,这就是它的全部用途。“当我按下按钮时不想执行”:你是如何推断它的愿望的?你也应该遍历Listbox1.items这是整个代码还是遗漏了什么?什么时候加载了peps.recipients?”假设我向listbox添加了2个项目,那么ObservableCollection包含2个字符串,对吗?“不,请不要这样说,甚至不要这样想。要添加项目,请将它们添加到ObservableCollection。迭代ObservableCollection中的项目。如果要删除项目,请将它们从ObservableCollection中删除。列表框只向用户显示ObservableCollection中的内容,这就是它的全部用途。“当我按下按钮时不想执行”:您是如何推断它的愿望的?您还应该遍历Listbox1.Items