wnd.Show(Me) not working in WPF - wpf

With my old programmes of multiple-windows, I used to use this parameter
wnd.Show(Me)
This code kept wnd above the first Window. Anyway in WPF it gives error
"Too many arguments for 'Public Sub Show'"
How can I do in WPF having the same advantages?

You can set the Window.Owner property in WPF to accomplish this:
wnd.Owner = Me
wnd.Show()

The function you're trying to use doesn't exist. The Window.Owner property is what you're looking for.
wnd.Owner = Me
wnd.Show()
You might also want to be using Window.ShowDialog if you're trying to keep it on top and modal - "This code kept wnd above the first Window" makes it sound like you're probably going for modal.

Related

Possible to pass WPF form by interface?

I know I have done this with Winforms and thought I had done it with WPF forms but it doesn't seem to be working for me now.
AKA...I love MVVM but sometimes I just want to throw up a prompt in the middle of my Viewmodel logic. In those cases I ask the View to pass me a prompt just in case I need it. The Prompt Implements an interface with the simple methods and properties I need (ex: ShowDialog, DialogResult etc...). I have also used Events to throw the duty back to the UI to prompt but sometimes it just seems cleaner for the VM to prompt without knowing what the view is via an interface.
So, did something change with WPF forms or I am just mistaken. I get a "Value of type 'PTO_DayDetail()' cannot be converted to 'iBasicPrompt'" build error when trying to pass a Form that Implements iBasicPrompt to a method expecting iBasicPrompt.
Public Class PTO_DayDetail
Implements MBS.Core.Interfaces.iBasicPrompt
End Class
Public Sub Add(... Prompt As MBS.Core.Interfaces.iBasicPrompt)
Yeah, I did something wrong (not sure what as I went a different route). I built a test app to post here and it all worked fine. Hmmmm. Weird.

Use a control in MainWindow from a nested page WPF VB.NET

I have a program in visual basic .net and WPF that uses a series of pages displayed in a Frame control on my main Window, pages are navigated from controls in the main Window outside of the frame and I have all that working ok.
I also have a MediaElement control on that main Window and I need to be able to change the source property of this control from the user clicking on elements in the pages. Every time I have tried to do this so far i have run into an error.
Right now I have a Public Shared function within main Window that is called from the control in the page, the code on the control passes on the URL to be loaded into the source property to the function, then the function is supposed to pass the url into the source property and tell the MediaElement to play.
The error I am getting when trying to achieve this is:
Cannot refer to an instance member of a class from within a shared
method or shared member initializer without an explicit instance of
the class.
Please help, how could I achive this?
Are you getting an instance of the main window or the media element inside of your shared function? If not, this is you problem.
You might stick the media element into a shared variable when the main window is loaded or you can access your main window using the rootvisual of your application.current.app.rootvisual.
If your issue is something else, please post the code in your shared function that causes the error.
Thank you very much Steve for your answer, however I found how to do what I need on another post after idly trying some different search terms today.
Someone known as Jean on another post: https://stackoverflow.com/a/16781963/2668533 suggested something that worked for me, by using application.current.windows:
Dim parent As MainWindow = Application.Current.Windows(0)
parent.player.Source = New Uri(url)
Apologies for seeming such a novice but WPF is completely new to me and I have never encounterd a need for anything similer using WindowsForms.

Access windows by name

I am quite new to WPF, coming from the Delphi world. I solved the problem below (albeit painfully) in the Delphi world, and hope there is a more elegant solution in the WPF world.
I need to read in an XML file containing a menu "tree", which has the window names in it as well as the menu prompts, and then be able to "show" a window based on having its name.
For example, a segment of the menu, with two choices, might have XML like this:
<MenuLeaf>
<Header>Product information</Header>
<MenuLine>
<Prompt>Product Master File</Prompt>
<WindowName>Products.xaml</WindowName>
</MenuLine>
<MenuLine>
<Prompt>Inventory Data</Prompt>
<WindowName>Inventory.xaml</WindowName>
</MenuLine>
</MenuLeaf>
So when the user makes the "Inventory Data" choice, I will know that I want to do a "show" of the window Inventory.xaml ..... but I only have the literal string "Inventory.xaml".
I will have hundreds of these forms, and the XML file can vary from time to time - so it's not effective for me to have the standard code of
Dim window as New Inventory
window.Show
for each of the several hundred windows.
What I need is something that does
Dim window as New {go out and find the Inventory file with name Inventory.xaml}
window.Show
I have searched endlessly for this with no luck.
I think the path to solution is to use Reflection, which will allow you to dynamically find/invoke your classes. Say your Namespace is MyNs, then you must have a 'Products' Class within it that correspond to the 'Products.xaml' file. To find it, use MyFoundType = MyNs.GetType("Products")
Then get default (or other if you like) constructor for this type : MyFoundType.GetConstructor(). Then invoke the constructor (with arguments if needed) --> you now have your window as an Object.
Cast it to a window and call its Show method, and you're done.
http://msdn.microsoft.com/en-us/library/y0cd10tb.aspx
http://msdn.microsoft.com/en-us/library/h93ya84h.aspx
http://msdn.microsoft.com/en-us/library/6ycw1y17.aspx
You need to use the XamlReader object, which parses XAML at run-time and creates the object.
var rdr = XmlReader.Create(File.Open("Inventory.xaml"));
var window = XamlReader.Load(rdr) as Window;
window.Show();
The XamlReader.Load will return whatever the actual top-level element in the XAML specifies; if it's a Window you can just .Show it. If it's something else, you'll need a container to place it in. For example, you might have a Window with a Border element in it and do:
var control = XamlReader.Load(rdr) as UserControl;
var window = new MyHostWindow();
window.ContentBorder.Child = control;
If you don't actually know the type of element in your XAML you can usually use FrameworkElement, which is the base class for all the visual elements, though you won't get Window-specific behavior from that.

WPF search windows - like tabPages in tab control

I have idea to implement my wpf windows like TabPages in tab control. It is possible to do that dynamically in c# code.
In Example i have Menu in main window. Some Menu items calls search type windows. Is it possible to do such a thing in C# code (SomeMenuItem_Click): this code adds new tab in tabControl of main window.
If there are no search windows called -there is no tab's shown, if there are many search windows called - there are many tab's.
So how do I code this?
And whats the technique with the windows? I suppose that my search type windows must be implemented like some UserControls. I think its no a good idea to implement that like simple wpf windows. I have tried to do that by using Marlon grech "Blend like UIs using DOCKY", find at:
http://marlongrech.wordpress.com/2008/01/29/create-blend-like-uis-using-docky/
But I failed, dont find the way how to add controlls in code dynamically, not in xaml.
I would appreciate code examples to illustrate how to achieve this.
Is it possible to do such a thing in C# code (SomeMenuItem_Click): this code adds new tab in tabControl of main window.
Yes. The basic pattern is:
TabItem newItem = new TabItem();
tabControl.Items.Add(newItem);
You'll obviously need to set the relevant properties of your tab item (such as the Header and Style) but that should get you started.
You'll then need to create any controls you want to show and add them to the tab item itself (or more correctly - a container within the tab item).

Setting DialogResult only after ShowDialog() in WPF

I have a window that I sometimes open using Show() and sometimes using ShowDialog(). In the second case, the returned dialog result is important for me. But if I set the DialogResult after calling Show() I get an InvalidOperationException. Is there a way to find out which method was used to open the window and set or not the DialogResult accordingly? Or is there another way?
Of course I know I can catch and ignore the exception, but I don't like this solution.
Use System.Windows.Interop.ComponentDispatcher.IsThreadModal inside the window to determine if it runs on a modal thread or not.
If you look at set_DialogResult in Reflector, it checks _showingAsDialog to determine whether the dialog is modal. Unfortunately this is a private field.
Do you always construct a new instance of the window before calling Show()/ShowDialog(). If so, you could pass an argument to the constructor indicating how it is to be shown.
You can use the Form.Modal property to check the kind of usage.
In the case of using Form.Show() you have to use another way to let the caller know of any results of the Form.
Is there a reason to use both ways of showing the form?
How about just setting this.DialogResult = DialogResult.blah in the form closing event?

Resources