How to clean another Window textbox? - WPF

Asked

Viewed 223 times

1

Follows code:

MainWindow mainwindow = new MainWindow();

mainwindow.listbox1.Items.Clear();
// tentativa
//mainwindow.listbox1.ItemsSource = null;

I try to clean up listbox from another form... nothing happens.

Some solution ?

  • Dude... I don’t think I’d be duplicated.. The other questions aren’t with WPF ? or am I mistaken?

1 answer

1


You are creating a new instance of Window, consequently the control will be new and etc!

To access controls in other windows with WPF, you need to declare control as public first, for example:

<TextBox x:Name="textBox1" x:FieldModifier="public" />

Then you can search all the windows in your application doing so:

foreach (Window window in Application.Current.Windows)
{
    if (window.GetType() == typeof(MainWindow))
    {
       (window as MainWindow).textBox1.Text = "";
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.