How to clean textbox from another Window? - WPF [duplicate]

1

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?

    
asked by anonymous 14.12.2017 / 03:06

1 answer

1

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 = "";
    }
}
    
14.12.2017 / 09:03