I have a user control that I have set as the Source of a Silverlight Frame. When I get the Parent of the user control in it's constructor, it returns Null. Is there an event of UserControl that is called after the control is loaded in the frame?
What about the Loaded event. If I understand your question correctly the Loaded event should be what you need to attach a handler for. That event fires after the object has been constructed, and added to the visual tree. So at that point it should have it's parent references set correctly.
Related
I need to access the below property of WebBrowser control when it is loaded completely.
webBrowser1.ActiveXInstance
this is null if the control is not loaded completely.
What property/event can I check like Form_Load?
There is WebBrowser.LoadComplete event for top-level only (the WebBrowser here I guess). If you're looking for an non top-level element, DocumentCompleted event occurs for all elements.
As ActiveXInstance is inherited from WebBrowserBase, there is also the Control.HandleCreated event, being fired when the control is displayed for the first time, though I don't know if it is applicable here.
(Sadly I'm not able to give the class and event links, but should be possible to find them yourself, right?)
Normally, if you want to set properties on a form, you can construct the form, set the properties and then showthe form. This gives you time to set the properties before the Load event is raised.
Code would look like follows:
MyForm form = new MyForm();
form.PropertyA = ...;
form.PropertyB = ...;
form.ShowDialog(this);
A user control also has a load event. If you put this user control on a form, then the Load event of the user control is raised at the end of the form's InitializeComponent(). So during construction of the form, way before the properties of the form are set.
If one of the properties of the form must be passed to the user control, then you are too late.
The solution I use now is give the user control an initialize function, that would do the things the user control would do during the Load event. This initialize function is called during the form's Load.
Although this works, this seems like bypassing Microsoft's way of initializing forms.
What is the proper way to load a user control if it needs some property values that are only known at run time?
I have a Silverlight control (ChildWindow) that I want to receive all key down events. The problem is that if I simply say
this.KeyDown += new KeyEventHandler(EventDetailsPopup_UC_KeyDown)
it won't work because the event is routed to all of its child controls. How do I receive Key or Mouse events from the UserControl or ChildWindow level? Thanks
You do not describe what you mean by "it won't work", but I can at least give you some background.
Silverlight supports the concept of Routed Events. When a routed event is fired on a child control, it is passes up the logical tree, firing on the control's parent, then the parent's parent etc ... until the root visual is met. The list of routed events is detailed on this MSDN page. This is called bubbling.
It looks like you want to prevent a child control from seeing this event? i.e. you want to cancel it by setting it as handled. Unfortunately this is not possible because the child control will always receive the event first. To support this you require a feature called tunneling, where a 'preview' event first tunnels from parent to child before the bubbling event is fired. This is a WPF-specific feature as described in the MSDN page referenced above.
Or if "wont work" means that you are not getting the event.
It may be beacause some child element has setted Handled parameter of eventarguments to true.
But you can still register for listening handled events in code lets say in constructor of your childwindow:
this.AddHandler(KeyDownEvent, (KeyEventHandler)YourHandlerFunction, true);
Is there a way to suppress the treeview_AfterSelect() event so it isn't called during form.show().
I have an application that is an MDI Container. One of the child windows contains a treevew. What the user selects on the treeview determines which child windows are shown. Due to a custom control I'm using the treeview form is also one of the windows that is closed and recreated. I've managed to mute the event handler and select the required node and then reenable the event handler in the constructor, but when the form is later shown the AfterSelect event is fired. Which is unwanted behavior in my situation.
Thanks in advance
The easiest approach is to use a member variable (e.g. "bool initialised"). It'll default to false.
At the end of your form's Shown event handler, set it to true.
In your AfterSelect, ignore the event if (!initialised)
I want to add event handlers programmatically to the server controls rather than using their predefined OnClick properties, etc. But which would be considered a better practice for defining handlers:
Define them in Page_Init
Define them in Page_Load
...and why?
Page_Init
Everything that has to be maintained between page cycles should be declared in Page_Init, not Page_Load.
edit All the initialization, like adding event handlers, and adding controls should be added during initialization, as the state is saved between page cycles. Handling with the content of controls and the viewstate, should be done in Load.
Check also http://msdn.microsoft.com/en-us/library/ms178472.aspx.
Init
Raised after all controls have been initialized and any skin
settings have been applied. Use this
event to read or initialize control
properties.
.
Load
The Page calls the OnLoad event method
on the Page, then recursively does the
same for each child control, which
does the same for each of its child
controls until the page and all
controls are loaded.
Use the OnLoad event method to set
properties in controls and establish
database connections.