Passing Input events from HwndHost to underlying WPF controls? - wpf

is there a way to pass input events (primarily mouse, but eventually keyboard input too) that occurs on a HwndHost back to the underlying WPF controls (e.g. a panel) ? i can hook up to WndProc within the HwndHost and recveive the windows messages.
can i manually create a routedevent for a mouse click and send it to the parent so it bubbles up?
and ideas would be appreciated.
thanks
jkersch

If you still require an answer: yes you can. Look at the Microsoft example here: http://msdn.microsoft.com/en-us/library/ms752055.aspx
In that example, the HwndHost derived class 1st creates a 'sub' window and then creates a control (ListBox) in that window. A HwndSourceHook is then added that catches messages (in the example, selection change messages from the ListBox) and uses them in the WPF context.

Related

Winforms Databinding and multiple Forms

i have a main window that contains multiple UserControls, arranged as tab pages and tab groups (much like Visual Studio allows to have two or more editors visible at the same time).
I also have the possibility to open such an UserControl into a seperate floating window.
One of these UserControls contains simple form fields (e.g. text boxes). These text boxes are bounded with common databinding to an object / property. The binding mode is OnValidation (not on OnPropertyChanged).
When I switch the focus from this User Control inside the main window into another UserControl in the Main windows, the validation is automatically performed and the databinding is finised / the changed text will be set on the model object / property that is bounded to that text field.
But if I switch the focus to an UserControl which resides in another (floating) window, the databinding is not finished since no validation is performed.
I know that I can handle this manually by triggering ValidateChildren etc, but this seems to my the wrong way / is ugly.
Is there a "correct" / clean way to solve this issue? I want that the validation is performed as soon as the UserControl loses its focus or the window gets deactivated.
One information: On of my UserControls contains a TreeControl. If I edit a tree node label, and when I switch the focus to another (foating) window, the label edit is finished automatically. I want the corresponding behaviour for usual form fields regarding binding...
Thanks for help!
There is no automatic way to do this. From the point of view of the control, it still has the focus (if you click the title bar or Alt-tab back to the main window, you will notice that the focus remains in the same control). Its just that the form the control is on is not active. If you want it to save changes when your form is deactivated, you must manually trigger it. The best way to do that is probably to override the OnDeactivate method of the form.
protected override void OnDeactivate(EventArgs e)
{
base.OnDeactivate(e);
this.ValidateChildren();
}

Getting notifications about controls creations in WPF

Is there a way in WPF that I can get notified whenever a WPF control is created or destroyed?
(For example an event I can register to "Control.ControlCreated"...)
What about Loaded and Initialized events of FrameworkElement class (and their counterparts)?
They are not routed events but with some styling and EventSetters they can be hooked to all required controls. The rest depends on what you need to do in event handler.
Other than that, I don`t see any common control events that can satisfy your requirements.

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.

Silverlight combo popup not displayed correctly when window resizes

If I create a ComboBox from scratch, when the window is resized, the content is correctly displayed above/below, etc.
Why this doesn't happen when you create your own Combo-derived class? What am I missing?
Controls aren't magical. They need to be told that things happen (call a method on them) or look for them to happen (register for an event).
Find your Application.RootVisual and register for a SizeChanged event. Then resize your control. If it is in browser you may need to create a javascript bridge (hopefully not) that tells your SL object when the browser resizes.

Trap keyboard input in a WindowsFormsHost control in WPF

I have an ActiveX control inside a WinForms user control. My WinForms app loves it!
Now, moving over to WPF, I use the user control in a WindowsFormsHost control. Works great..., but I want to treat this control as a single element so the user can neatly hit TAB over the existing WPF controls AND this user control NOT to 'go inside' it. i.e. just treat it as a single control like all the others.
I think what i need is the ability to trap the keys, and in the event handler simply move focus to the next control in the sequence, but I can't seem to trap any keyboard input. Ive tried the WPF PreviewKey.. events and the like, but once the tabbing gets to the control, it seems to stay inside it and WPF events are ignored.
I couldnt find anything on this in many WPF books and the net. Can anyone suggest a way ?
Thanks,
Jack.
Can't you create some sort of a filter by doing a preview mouse down on the panel or window (whatever is the parent of your controls), this way the panel will catch it before the user control and you should set e.handled to true, and if the user control raised the tab event, keep pushing the focus until you get another control. Preview and e.Handled=ture should solve the problem.

Resources