How can I iterate to the window control from a form - wpf

How can I iterate to the window control from a form in the below hiearchy?
Windows.Forms--> WPF UserControl --> WPF Window(Using ShowDialog).
LogicalTreeHelper.GetChildren does not catch the Window object.

A window that is opened by calling Show does not automatically have a relationship with the window that opened it; specifically, the opened window does not know which window opened it. This relationship can be established using the Owner property and managed using the OwnedWindows property. FRom here - Window.Show Method()
But you can't set Owner for your WPFWindow because 'Owner' property can get only WPFWindow but you have as owner WinForms window. Maybe you can just save WindowControl as instance variable in you class so then you can get it any time you want.

Related

WPF MDi With Background Element

How can add Elements inside wpf MDIcontainer section(Like background).
I need this
I would suggest using a DataGrid for your MDI. Bind an ObservableCollection as its ItemSource. Then you can add a new Employee to that Collection. If INotifyPropertyChanged is implemented correctly it will AutoUpdate your Collection and the Content will be shown.
NOTE: Your Employee window has NOT to be shown via .ShowDialog() cause this will freeze the background Window.
This is no problem at all when hitting the save button is closing your employee window.

Get parent window from popup element

I have popup on my current application. There is one image inside popup control. Now when I click on that image, I want to have object of main app window.
When I write this, it gives me null.
Window window = VisualTreeHelpers.FindVisualParent<Window>(Image);
And When i write this, it gives me object of POPUP windo, not application window.
window = Window.GetWindow(Image);
How would I get main application window from Popup Image control ?
Application.Current.MainWindow
MainWindow is automatically set with a reference to the first Window
object to be instantiated in the AppDomain.
You can specify a different main window by setting MainWindow
assigning another Windows object to the MainWindow property.
Link to MSDN page is here.

Closing WPF dialog box from contained UserControl

Hi I have a WPF application with various UserControls that contain key functionality. I want to be able to show say a FileManager UserControl in a tab on the main application, or have a dialog pop up when required, that contains the same UserControl.
It seemed like a good idea to create a modal Window and set its Content to the FileManager Usercontrol. But when I am finished with it, I am not sure how to close the containing Window, using a button on the UserControl. Is there an elegant way of doing this without having to store a reference to the Window in the UserControl?
Thanks for any advice!
Create an Event which is called when the respective button on the user control is clicked. That way, the containing control can react to the event in the appropriate manner. For example, a dialog could just close itself.
Is closing a window something that is integral to the functionality of the control in all the contexts where the control is hosted? E.g Does closing a window apply to the case where the control is hosted in a tab of the main app?
If not then you might be better off separating window closing code out of the UserControl in into the window/dialog that is hosting it - using events or whatever to tie the two together.

how to open a WPF dialog from a WinForms menu

I have a WinForms menuitem and I need that on menu click the new WPF dialog is loaded.
How do I do this?
Thanks
You should be able to display it by creating a new instance of the WPF class for the dialog, and then calling its ShowDialog() method.
The only trick is properly setting the owner of the WPF dialog. You can't just set the Owner property directly, since that requires a WPF window. However, you can use the class System.Windows.Interop.WindowInterpHelper to get around this:
MyWpfDialog dialog = new MyWpfDialog();
new System.Windows.Interop.WindowInteropHelper(dialog).Owner = this.Handle;
dialog.ShowDialog();
(I got the code sample from http://blog.stpworks.com/archive/2009/07/02/setting-wpf-dialog-owner-from-within-winforms-application.aspx.)

WPF - call a window function from a child page

I have WPF window that uses a dockpanel and the menu control. I have the code to create the menu options based on a user ID.
Within this window, I have a frame that contains a WPF page. I carry out all the authentication on the page and then have a user ID for the window to use. However, I cannot get the parent window to "refresh" and create the menu bar with the new ID information. When the window loads, I do not run through the commands to display the menu bar. I have tried putting that in its own, public, function and calling it from the page but that does not seem to work.
There must be a window method that I'm missing that can make the menu bar display based on the call from the page.
It sounds like you're still thinking in procedurally, in WinForms style. What you describe would be necessary in WinForms, but in WPF it is usually much easier: just use data binding. As long as your menu items are generated from a "UserID" dependency property (or enabled/disabled based on it), then all you need to do is set the UserID DependencyProperty and the UI will update itself with no additional code.
Here is how to get the UserID into a DependencyProperty of the Window or a context object:
In your Window or a context object create a "UserID" DP
Make your Window or your a context object the DataContext of the page
At the end of the authentication code, set DataContext.UserID (or alternatively create a UserID property on the page, and have the Window bind to it with a two way binding)
Once you have the UserID in a DependencyProperty, there are many ways to update the menu items automatically whenever it changes:
In each menu item, bind its visibility to the UserID DP on the window using an appropriate converter (using the converter parameter to distinguish between items), -OR-
Use a converter for setting ItemsSource so you filter your items, -OR-
Create a PropertyChangedCallback that sets a filter on the CollectionView you use for menu items, -OR-
Some other technique (there are many other good ways to do this)
For typical situations we're talking about less than 10 lines of C# here, not counting the DependencyProperty declarations.

Resources