开发者

How to manipulate a window object from another class in WPF

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 t开发者_运维百科o 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!


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.


Pure WPF solution, but also may be easiest in your case, is using a Data Binding in WPF.

Every form's control is binded to some data on ModelView (pure MVVM approach) or to data (more or less like yuo can do it in WindowsForms). So the "only" thing you have to do is to read/write data binded to controls on UI of that form.

For example, you have TextBox on Windows and want to read a data from it. This TextBox is binded to some string property of the class that is responsible for holding the data for the controls on that form (just an example, in real world could be 1000 other solutions, based on developer decisions). So what you need, is not to say: "window give textbox" and after read TextBox's content, but simply read binded string property.

Sure it's very simply description of a stuff. But just to give you a hint. Follow databinding link provided above to learn more about this stuff. Do not afraid of a lot of stuff there, it's after all is not a complicated idea and also pretty intuitive. To make that stuff to work in simply case you will not need to make huge efforts by me. The stuff becomes really complex when you end up into real world applications.


This will get all active windows:

foreach (Window item in Application.Current.Windows)
{

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜