简体   繁体   English

wpf列表框项目从另一个wpf窗口添加

[英]wpf listbox Items add from another wpf window

Does anyone know how to add items to the WPF Listbox (window created on blend) from another WPF window ? 有谁知道如何从另一个WPF窗口将项目添加到WPF列表框(在混合时创建的窗口)?

I have the following codes 我有以下代码

mainpage mp = new mainpage();
        mp.listbox1.Items.Add(namebox.Text);

Thank you guys 感谢大伙们

two solutions: 两种解决方案:

  1. Create a constructor with parameters and add them in the constructor 创建带有参数的构造函数并将其添加到构造函数中

    mainpage mp = new mainpage(namebox.Text); mainpage mp =新的mainpage(namebox.Text);

public mainpage(string newItem){ this.listbox1.Items.Add(namebox.Text); 公共主页(字符串newItem){this.listbox1.Items.Add(namebox.Text); } }

  1. Make the list public and access the list 公开列表并访问列表

You can use the Application -class in System.Windows -namespace. 您可以在System.Windows -namespace中使用Application -class。 Use 采用

System.Windows.Application app = System.Windows.Application.Current;
System.Windows.WindowCollection wincol = app.Windows;
//iterate through collection and find your window

Of course, you'll Need to take care of threading, as usually every window is created in ist own thread. 当然,您将需要注意线程处理,因为通常每个窗口都是在自己的线程中创建的。 See MSDN for further reference. 有关更多参考,请参见MSDN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM