Follow the code below:
MainWindow mainwindow = new MainWindow();
mainwindow.listbox1.Items.Clear();
// tentativa
//mainwindow.listbox1.ItemsSource = null;
I try to clean listbox
from another form ... nothing happens.
Any solution?
Follow the code below:
MainWindow mainwindow = new MainWindow();
mainwindow.listbox1.Items.Clear();
// tentativa
//mainwindow.listbox1.ItemsSource = null;
I try to clean listbox
from another form ... nothing happens.
Any solution?
You are creating a new instance of Window, consequently the control will be new and so on!
To access controls in other windows with WPF, you need to declare the control as public first, for example:
<TextBox x:Name="textBox1" x:FieldModifier="public" />
Then you can search all the windows in your application by doing this:
foreach (Window window in Application.Current.Windows)
{
if (window.GetType() == typeof(MainWindow))
{
(window as MainWindow).textBox1.Text = "";
}
}