How to manipulate a window object from another class in WPF

16,850

WPF windows defined in XAML have their controls publicly accessible from other classes and forms, unless you specifically mark them with the x:FieldModifier attribute as private.

Therefore, if you make an instance of your main window accessible in another class, be it a Window or anything else, you'll be able to populate controls from within this second class.

A particular scenario is when you want to update the contents of a control in your main window from a child window that you have opened on top of it. Is such a case, you may set the child window's Owner property to the current, main window, in order to access it while the child is visible. For instance, let's say you have defined these two windows:

// MainWindow
<Window x:Class="TestApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ListBox Name="mainListBox" Height="250" HorizontalAlignment="Stretch" VerticalAlignment="Top"/>
        <Button Content="Open Another Window" HorizontalAlignment="Center" VerticalAlignment="Bottom" Margin="20" Click="OpenAnotherWindow_Click"/>
    </Grid>
</Window>

and

// AnotherWindow
<Window x:Class="TestApplication.AnotherWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="AnotherWindow" Height="300" Width="300">
    <Grid>
        <Button Content="Add New Item to Main Window" HorizontalAlignment="Center" VerticalAlignment="Center" Click="AddNewItem_Click"/>
    </Grid>
</Window>

each in its own XAML file.

In MainWindow's code behind, inside the button click handler, you show an instance of AnotherWindow as a dialog and set its Owner property to MainWindow's instance:

private void OpenAnotherWindow_Click(object sender, RoutedEventArgs e)
{
    AnotherWindow anotherWindow = new AnotherWindow();
    anotherWindow.Owner = this;

    anotherWindow.ShowDialog();
}

Now, you can access the MainWindow's instance from AnotherWindow's Owner property, in order to add a new item to the ListBox control defined on it, in the button click handler in AnotherWindow's code behind:

private void AddNewItem_Click(object sender, RoutedEventArgs e)
{
     MainWindow mainWindow = Owner as MainWindow;

     mainWindow.mainListBox.Items.Add(new Random().Next(1000).ToString());
}

It simply adds a new random number to the ListBox, in order to show how the code accesses and modifies the control's data in MainWindow.

Share:
16,850
Admin
Author by

Admin

Updated on June 27, 2022

Comments

  • Admin
    Admin almost 2 years

    I'm new in WPF and C#. I know a lot of VB.NET and I'm used to the way when I call a form object like textboxes, etc. I'm calling it from another form. Now, I'm using WPF, I'm confused. Because I have a Main Window. And I want to add and item to a listbox in the Main Window from a Class. In VB.Net , its just like this.

        IN FORM2
    
        Form1.Textbox.Text = "";
    

    Wherein I can't do it in WPF. Can someone please Help me. Thanks!

  • Ms. Nobody
    Ms. Nobody almost 11 years
    Dunno why but it keeps giving me exception that mainWindow is null :| Any ideas where might me the problem?