I have a window that is hosting a user control. I wish to pass (raise) events in the parent window that should reach the user control.
How do I define events in the parent window that the child user control can also receive.
As I recall the steps should be:
Define a delegate in your parent window
Define an event in your parent window
Lets say in your window Loaded event attach the usercontrol to the
event, eg -
this.MyEvent += new
this.MyDelegate(this.UserControl1.SomeMethod);
Take note that
SomeMethod must match the MyDelegate definition.
Raise the event in your parent window per application logic.
For examples of how to define delegates/events then google is your friend ;) Also don't forget to unregister your usercontrol from the event, when it's no longer required (when the window closes).
Related
If there is a button within a group box on a win form, and when the "click" event occurs, then who is actually calling the Event. Is it the button contorl or its parent i.e. GroupBox.
If you'd have written a native Windows GUI program then it would be the group box that got the click message. Buttons send notification to their parents. But that gets re-routed in Winforms, both through sub-classing and having the container window reflect messages back to the child control.
Events always originate at the control. Their OnClick() method in case of a click. You however still handle the event at a higher level window. Typically the form, not the groupbox. The sender argument of the event handler passes a reference to the control. Having a choice over exactly where you handle the event is a major advantage over the native way. It makes controls highly adaptable and composable.
I created new WPF Application witn two windows.
MainWindow.xaml
Window1.xaml
Added one button in MainWindow.xaml and wrote the following code in the click event of the button:
Window1 w = new Window1();
w.Show();
I clicked that button 2-3 times and it opened multiple instances of Window1.
Everything fine till here.
Now, I closed MainWindow and it did not close the instances of Windows1.
I was thinking that since MainWindow is the one that owns the Process and if it closes, rest of the child windows will close automatically.
I did the same project in WinForms application and the result was completely different. On closing the MainWindow, it did close rest of the windows.
Am i not understanding the concept clearly? Can someone tell me who owns the process or the main thread in WPF?
The other windows are not child windows of your MainWindow unless you set window.Owner = mainWindow;
Further there is the Application.MainWindow property which sets which window actually is treated as "the" main window. This affects the Application.ShutdownMode if set to OnMainWindowClose.
Excerpt from the Window.Owner reference:
When a child window is opened by a parent window by calling ShowDialog, an implicit
relationship is established between both parent and child window. This relationship enforces certain behaviors, including with respect to minimizing, maximizing, and restoring.
When a child window is created by a
parent window by calling Show,
however, the child window does not
have a relationship with the parent
window. This means that:
The child window does not have a reference to the parent window.
The behavior of the child window is not dependent on the behavior of
the parent window; either window can
cover the other, or be minimized,
maximized, and restored independently
of the other.
To allow you to create a relationship
between a child window and a parent
window, Window supports the notion of
ownership. Ownership is established
when the Owner property of a window
(the owned window) is set with a
reference to another window (the owner
window).
The application root class: System.Windows.Application
WPF Threading Model
My application is a UserControl which has a grid whose content is loaded dynamically. Essentially, I maintain a list of child objects and replace the grid child whenever needed. I now need the child object to subscribe to an event that is raised by the UserControl. If it was the other way round I would have just done child.property +=.... How do I get this to happen?
If the child object already implements the features that you need to trigger, you can have the UserControl subscribe to the event on its own, then call the child functionality from the handler.
I need some help to implement a common behavior in some controls.
In my WPF application, I have a main form that contains a panel and a button:
Ok
The button will run a Save method when clicked.The Save method reads some data from the form and saves the data to a database.
The panel is populated with dynamically created controls (such as textbox, dropdownlists, etc). The main form instantiates a MainViewModel class. This MainViewModel class instantiates a class called UIFactory. So we have 3 levels here.
In the UIFactory class the controls is being created. The Panel from the main form is sent as a parameter to a method in the MainModelView class called GenerateUI. This GenerateUI method in the MainViewModel class calls a GenerateControls method on the UIFactory class that takes the same panel as a parameter. The GenerateControls method in the UIFactory class then adds dynamically created controls on the panel.
What I want to achieve is that whenever the user hits ENTER when he is typing in one of those dynamically created controls e.g a textbox, I want that behavior to be the same as clicking on the button in my main form. But how do I do that? I thought of implementing Routed events on my controls, but I can't figure out how to do it. Could you please advise me on how to achieve my goal?
Best Regards,
OKB
Maybe the Keyboard.KeyUp attached event might help you. You could set it on the main panel which contains the dynamically created controls and then perform the save operation if the pressed key was the ENTER key.
I did manage to create a work aroind to my problem:
What I did was to create a custom user control(let's call it a container). This control is hosted in my wpf application using the WindowsFormsHost instead of the panel. Then I add the dynamically created user control to my new custom user control (the container) and add a KeyEventHandler on each child control's KeyUp event.
I created a custom event and event handler in my container that will catch all KeyUp event from the child controls, check if the e.KeyValue == 13 (ENTER) and then raise my custom event from the container that will be handled in my wpf form. Ugly as h*ll, but it works.
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.