I have a WPF app with a Window (RootWindow) with a Toolbar and a Frame (ContentFrame). Initially the Toolbar is hidden.
I load a Login UserControl into the Frame and when the user correctly logs in I'd like to close the UserControl and then make the Parent Window toolbar visible.
Seems such a simple thing to do.
However, you cannot close a UserControl from within the UserControl. So how do I break out of the UserControl so I can remove it from the RootWindow (ContentFrame.Source=Nothing) and also make the toolbar Visible.
I can get a handle for the Parent Window with the following code but I cannot access the controls within it
Dim parentWindow As Window = Window.GetWindow(Me) 'Get a handle for parent window
Ideally, I'd like to be able to access Parent Window Controls from within a Child UserControl or at least be able to Trigger an event in the Parent Window from the Child UserControl.
To find the parent in the hirearchy you can use this code:
http://www.hardcodet.net/2009/03/detecting-double-click-events-on-the-wpf-datagrid
Although the problem solved in above article is for DataGrid, the code to find parent is generic enough and should work in your case.
Related
I'm using MVVM. I have a tabcontrol and numbers of button in my MainView.
When I click some button it will load child view in tabitem. This child view also having tabcontrol.
Now when I click to some other button the sub child view should be loaded in to childview tabcontrol as tabitem.
What is the best practice for setting up a navigation like this?
I don't want to use EventAggregator. Is there another way to do this?
So how to execute command in child view to load sub child view in child tabcontrol as tabitem from mainview or main window?
You can always use Composite Commands.
Make the button commands composite, and have the child view register to those commands
By the way, is there a specific reason for not wanting to work with the EventAggregator?
I have one window and some userControls. When Window loaded, one of UserControls programicaly Hosted in Window.Content. Suppose, userControls have buttons that when clicked by user this Current UserControl Removed from window.Content and destoryed and another userControl added to parent Window.Content. I used Parent like this:
(Parent as Window).Content = new MyUserControl;
But after once changing parent userControl, result of Parent return null and previous userConrol did not destroyed.
Just created the attached sample to test what you are trying to do. Kindly provide some more detail about the code you have. Attached sample does exactly what you said without any null references.
Sample
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.
I would like to take an existing window (I know its window handle, it is not a winforms control), place it into a winforms container and also make it dock with something similar to DockStyle.Fill.
Now, I am able to reparent the window, but I cannot figure out the docking part. I can wrap the child window in a NativeWindow, but NativeWindow is not a WinForms Control class and as such does not have the Dock property.
Is there a way to wrap a generic window handle in a Control? Or do I absolutely need to track the parent control resize events and alter the child native window size accordingly by myself?
I facing very huge problem now,
that is I have usercontrol (created by me) which I have added on to a window...
on click of button I wann show another usercontrol on window, total how can I access the window object in my control (usercontrol)
Thanks all
You can use Window.GetWindow. However, you might want to consider a redesign where the Window is responsible for its content, not something hosted inside the Window.
You could do that by accessing the Parent property.
But why not raising an event from the UserControl which is handled in the window, and there you add the other control?
Or you could create an event and a delegate in the usercontrol and handle it in the Window.