Getting notifications about controls creations in WPF - 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.

Related

How do you bind a TextBlock text in the MainWindow to a property in a seperate User Control?

I'm having a problem with binding across User Controls. I have a Notification bar which is displayed over the entire application, but I want to change its text from within a User Control's View Model using binding. I've looked at many ways of doing it but I haven't been able to get it working.
Each XAML control has no code behind, and in a separate class I am creating a View Model. This is then assigned to the XAML's data context when it is initialized.
Can anyone help with this problem?
I would use some kind of Event system, such as MVVM Light's Messenger or Prism's EventAggregator
This would allow the ViewModel containing the notification message to subscribe and listen to events of type ShowNotificationEvent, and when it gets one it can set the value and show the notification.
Any area in the application that wishes to show a notification only has to raise a ShowNotificationEvent and pass it the message to display
You can use the great prism's EventAggregator here is a tutorial how to implement it

Capture all Click or Touch events recursively in WPF

am pretty new to WPF, but am looking to capture whenever anyone touches inside a window or any child controls.
If I capture the click event for a Window, only the windows inner space capture the click. It's child controls do not.
How do i recursively capture any click/touch event ANYWHERE on the screen in a full size window?
many thanks in advance
The routed event handling implementation in WPF is intended to give all controls in a nested hierarchy a chance to intercept and handle touch & mouse events. However, controls have the ability to prevent children from receiving the event notification.
There's a pretty good explanation of event routing here: http://nui.joshland.org/2010/04/why-wont-wpf-controls-work-with-touch.html
All controls receive a Preview event (for click or touch), and this cannot be prevented. After this, the event is 'promoted' to a regular Mouse/Touch event (touch is handled before click) However, if any control in the hierarchy for the 'click' (_MouseDown in WPF) event handling sets the Handled property on the event args to true, then the event will not be propagated any further.
Unless you are handling touch events or manipulations, or explicitly setting e.Handled = true in your code, then all controls in a nested stack should receive the _MouseDown event.
As noted in the comments below, some controls will set 'Handled = true' which would prevent their containers from receiving corresponding _TouchDown or _MouseDown events. However, they would all receive a PreviewTouch/PreviewMouseDown first.
Also note that handling touch events prevents handling of mouse events.

WPF objects removed from Canvas hang arround responding to events

I've got a flowchart-style application drawn on a WPF Canvas. (Basically boxes connected with lines). The boxes are hooked up to various events on the 'model'. I need to able to clear the Canvas, without destroying the model.
What I do is remove all the boxes etc from the Canvas' Children collection. This works fine visually, but the controls seems to live on in limbo somewhere in memory. When I later fire events on the model, the controls crash my application. (the now-invisible boxes 'expect' to be the Child of a Canvas).
How should I indicate the controls are no longer wanted? I want them destroyed, not responding to events and bindings. Are the bindings to the model preventing WPF disposing of them? Would setting their Datacontext to null help?
Cheers, Jeff.
Sounds like you may need to have the boxes implement IDisposable and call Dispose on the boxes as you are removing them. In the dispose method you should unhook your event listeners. Or if you intend to boxes to be used again you may need to just expose some method to RemoveListeners() and then a method to AttachListeners() when you want it back in view.
public void Dispose()
{
model.MyEvent -= LocalListener;
}

Listening to routed events / commands from a popup

I have a control that dynamically creates a popup.
The popup contains controls that fire routed events / commands, which I want to react to in the original control. The original control is set as the placement target of the popup.
Would you expect the original control to receive bubbled events? I know it's in a different visual tree, but I wondered whether they would be offered to the placement target. From my code it would appear not.
Can anyone suggest a way to handle this situation? Responding to events in a different visual tree. I was wondering if there was some control I could write that would sit in the root of the popup and act as a "bridge" into the originating visual tree?
Many thanks,
I have managed to get around this by adding my CommandBinding to the popup's CommandBindings collection instead of my control's.
As I do this in code at the point of the popup's creation, I can specify callbacks in my control, even though the binding is in the popup.

How can I get WPF binding to occur on controls which have not yet been shown?

I have a custom text box control which raises a routed event when its TEXT property changes. This text property is data bound to a property on our view-model object.
When I place this control on a TabControl page or Expander control, it appears as if data binding only occurs when the control becomes visible for the first time, therefore I never receive any of the routed events until I swap to the tab the control is on or expand the expander.
Is there any way I can force data binding to occur before the control is shown?
Sounds like you relying on the data binding to genreate the routed event is the wrong approach. Instead you need to have your Model or ViewModel generate an event when the text is modified and then you watch this event from an appropriate place in your View.
Not very likely. WPF is a fairly efficient framework and won't do any work that it doesn't absolutely have to. This includes scenarios like data binding. Why bother exercising a collection for a control that might not ever be shown?

Resources