How to prevent delay loading of a UserControl in a TabControl? - winforms

I have just discovered that UserControls in a TabControl will not load until the parent TabPage is selected. Is there a way to prevent this delay loading? I need to initialize the UserControls when the main form loads.

The TabControl does not treat its controls specially, in fact it is normal under all circumstances for the Load event on a UserControl to occur immediately before the control is displayed for the first time. The TabPage is responsible for showing the control, therefore it will only be 'loaded' when first selected.
To overcome this (perfectly normal) Windows Forms behaviour, you could move your initialisation code to a separate method and call it when the Form loads, or you could just place your initialisation code in the UserControl's constructor instead. Either way, you can perform your initialisation immediately.

You can invoke Tabcontrol's SelectTab() method for the tabs in your Form's Load event handler.

I was just searching how to achieve this default behaviour you describe. An application I support was not delaying the load of the tabs. Turns out the tabs were getting initialised in the load event instead of the constructor.
So if you add the tabs to the tabcontrol in the form load event all the controls in tabs will have their load events fired as a part of the TabPages.AddRange call

Related

PivotItem Loaded event fired twice

Windows Phone 8, Silverlight. I have a PivotItem that is declared in my page XAML under the ResourceDictionary of the page, because I don't want it to appear initially and there's no dynamic visibility for PivotItems. When the time comes, I take the object, remove it from resources and add it to a Pivot. It works, but the Load event is, for some reason, fired twice - once on page loading and once when I insert it into the Pivot.
I'd rather have my OnLoaded processing only once.
Is that by design? Is there a way around? Is there a similar event that's fired once upon object initialization (except for the constructor)?
I can work around that by introducing an init-once boolean flag, but still.
You can try OnNavigatedTo. A loaded event can fire on all most any item in the visual tree. If you're looking to collapse items on a pivotItem but no the pivotItem itself, then hook into the parent Grid container.
Ultimately, the Pivot control's children can be removed
myPivotControl.Items.Remove(item);
and you can add an item back in the same manner
myPivotControl.Items.Add(item)
However, I suspect you'll have better results using a different event. The parent pivot has event for adding and removing items. This link will take you to the documentation for the Pivot class. In addition, this link will take you to the PivotItem class.
You can make the PivotItem collapsed in the beginning and then make it visible when you need it.

How to preload a control with images?

I am working on a WPF application where I need smooth animations and transitions between pages.
On startup, I instantiate all my controls, with Visibility : Collapsed, and when I go from a control to another, I animate the two controls and change the visibility.
My problem is on a control with several images : the first time I display it, we don't see the animation (probably because it loads the images in the same time). When I load it a second time, there's no problem.
Is there a way to preload this control in order to always have smooth animations ?
In the codebehind you can have an event handler OnLoaded() for when the your control is loaded for the first time. Here you can load in the images or even have them start as Visible so they are loaded and at the end of the handler, have them go back to being Collapsed.

Show Loading Image when TreeView Node is clicked in WPF

The question is we have a WPF application, which has Tree View. On the click of node Report gets generated which has no time interval (I mean no clue how much time it will take). So I am planning to show a Loading.gif file on the window till the report is generated.
How can I make the image (.gif) visible while the main window process to show the report and after showing the report I need to hide the image.
Do you have any other alternate method to do so.
Appreciate your help in advance.
You can make use of Extended WPF Toolkit's BusyIndicator.
Here's a sample of how to make async multi-threaded treeview -> http://www.codeproject.com/KB/WPF/ThreadedWPFExplorer.aspx
The general technique for this is:
1) Create an IsBusy property in the view model for your window; make sure it raises PropertyChanged when it changes.
2) In the code executed when the item is clicked, use a BackgroundWorker to run the long-running task.
3) Before calling BackgroundWorker.DoWork(), set IsBusy to true. In the event handler called when the BackgroundWorker.RunWorkerCompleted is raised, set IsBusy to false.
4) In the DataTemplate for the window, add a Style with a DataTrigger bound to IsBusy, and use that to control the visibility of the image.
Note that you may be able to move IsBusy (and the long-running task and the BackgroundWorker) to the item view model instead of the window view model, and add a "Loading" animation to the DataTemplate for the item.
If you do this, the user can start multiple items loading simultaneously, and the entire application doesn't lock up just because one item in the TreeView got clicked on. (Of course, you have to deal with any multithreading issues involved in generating multiple reports simultaneously).

Does Visibility = IsCollapsed skip the data-binding part?

If I set a StackPanel or what ever bound area I have as 'Collapsed', does the data load?
Will it trigger its Loaded event?
Yes:
Your data bindings will be evaluated,
Your Initialized events will fire, and
Your Loaded events will fire
But in the section of the visual tree under the Visibility="Collapsed" element:
Your controls will not be measured or arranged
Your controls' Templates will not be applied
So the bottom line is, if you want to avoid loading data for invisible sections of your UI, don't load your data and set your DataContext until after the control is first measured. Also consider putting any complexity inside a template.

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