1

I'm sorry to post this question because there's a lot of relative subjects with this one but unfortunately none of them couldn't helped me !! so I have an application that contains two wpf windows (MainWindow() and Window1()), what i exactly want to achieve is to control the window1 properties from the MainWindow().. for example i want to clear a listbox items from window1 every time i click on Button1 from the MainWindow !! I also want to have a full access to the methods, fields and properties that exits in the MainWindow from window1.

2 Answers 2

2

What you could do is to add public properties/method that allows access to your Window1 class from MainWindow.

For instance, if you want to clear a list in Window1 from MainWindow, add the following method to Window1:

public void ClearList()
{
    // Clear your list here
}

Of course you'll need your MainWindow to know about Window1, but as the name suggests, I assume MainWindow is the main window so it creates the Window1 instance.

So you should have a reference to Window1 in MainWindow. In MainWindow, just call:

this.myWindow1.ClearList();
Sign up to request clarification or add additional context in comments.

5 Comments

so like that i don't have to create a reference of Window1 in the MainWindow like windwo1 wind = new window1() ?
You do have to. How would you show the Window if you don't? First create your instance, then call a method of your instance.
thanks I'll do that when i need to, but the ClearList() method is not working with me.. when i implement the ClearList() in window1 and when i try to call it in the main window like Window1.ClearList(); it fails !!
@Destro009 Yes, because you are calling the method to the class itself, not to an instance of the class.
but it's okey when i use this !! like, this.window1.ClearList() i hope it works fine !!
1

Where is the problem ? I dont see any question.

Maybe you want to start by saving a ref of Window1 in MainWindow.

Window1 window1 = new Window1();
window1.show();

From there do what you need done :

window1.foo();
window1.bar();
window1.foobar = "Title";

Edit : some clarification, because OP seems to be a beginner :

public class MainWindow
{

    private Window1 window1;

    public void CreateWindow()
    {
         window1 = new Window1();
         window1.show();
    }

    private void DoStuffWithWindow1()
    {
         window1.foo();
         window1.bar();
         window1.foobar = "Title";
    }
}

5 Comments

yes i've already done that and get the StackOverFlowException !! An unhandled exception of type 'System.StackOverflowException' occurred in *****.exe
Hum, you must have some recursive call somewhere. Please post code. Are you using any events ?
no, the exception is about Window1 window1 = new Window1(); ps: i declare the window1 as global variable !!
Global ? As in class you mean ? When are you initializing window1 ? In the ctor of mainWindow ?
yes in the public partial class, but i don't understand why every time i run the app a new instance of window1 runs also !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.