I have a function with one string parameter. Parameter is the name of existing wpf Window’s name. Now i want to create a instance of window by string parameter and want to call Show() function of that window.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It depends on whether the name you are given is the filename the window is defined in or the class name. Usually these are the same, but they can be different.
For example you might put the following in a file called “Elephant.xaml”:
If you do so, then the filename of the window is “Elephant.xaml” but the class name is “Pachyderm” in namespace “Animals”.
Loading a window given the file name
To instantiate and show a window given the filename:
So your method would look something like this:
And be called like this:
Loading a window given the class name
To instantiate and show a window given the class name:
So your method would look something like this:
And be called like this:
Alternatively you could include the namespace (“Animals” in this example) in the parameter to ShowNamedWindow instead of appending it inside the method.
Loading a window given only the title
This is not recommended, since it could be a very costly operation. You would need to get the Assembly, iterate all the types in the Assembly that are a subclass of Window, instantiate each one, and extract its Title property. This would actually construct (but not show) one of each kind of window in your application until it found the right one. So I would use the filename or class name if at all possible.