Show Loading Image when TreeView Node is clicked in WPF - 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).

Related

WPF: How to update UI without blocking animations

in my WPF application I have a ListView with a GridView as it's View. The ItemsSource of the ListView is bound to an ItemsCollection in the ViewModel.
This GridView has a lot of columns and the ListView can have a lot of items.
When the windows is loading or the user updates this ListView I show a loading animation while retrieving the new data from the server and updating the ItemsCollection in the ViewModel with the new data.
This retrieving of data is already done asynchronously, so the loading animation keeps spinning. But as soon as the data is retrieved and the binding is to update the ListView, the loading animation freezes until the update of the UI is finished.
How can I make my loading animation keep spinning even when the binding updates the UI?
Thank you for your help.
EDIT:
Well, that binding runs in the main thread, the same thread your animation runs.
Clearly my previous solution wont help you. You will have to find a way to reduce the time it takes to rendering view. Share some working code, maybe someone can help you.
Other alternative is to stop the animation and show something static. That way you hide the problem. Its not a great solution, but if you don't find other, this could your only one.

WPF loading panel and UI freeze

I have a mainwindow which displays a 'Please wait' panel containing a loading gif whenever I do some work in a Task. The gif plays fine when the data is being compiled, but when the INPC logic to refresh the UI controls is being executed (thus on the main UI thread), the app freezes for a split second and my gif does not play smoothly anymore.
For instance, I am following the MVVM pattern, I have a datagrid in my view which is binded to an ObservableCollection MyBindedCollection. In the constructor of my viewmodel which runs in a Task, I create a tmp observable collection and when the process is finished, I set the variable MyBindedCollection to tmp (thus not adding one by one my items to it). The INPC logic is of course in the setter of my property MyBindedCollection.
Is there anyway to prevent this? any idea would be appreciated.
#ZoolWay yes, no way around that I'm affraid. I'll just fade out my wait panel and thread.Wait enough time for the animation to finish before displaying my content. Thus it'll appear smooth. Thanks for your time.

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();
}

How to show some animation while control is rendering?

I have WPF ListBox that shows a lot of data. I need smooth scrolling, so I've set ListBox.ScrollViewer.CanContentScroll to False that disables virtualization. Now when I open the tab where this ListBox is placed, I see nothing for few seconds because ListBox is loading/creating items/rendering. I also have a control that shows some animation that indicates that application is running and user should wait a bit.
How can I show this control while ListBox is not available?
Add a Grid in the location of your list box and place inside it your ListBox and your animation control. This way they are placed in the same location. The animation control should be on the top of the z-order and so displayed. Once the ListBox has finished loading you would then hide the animation control and so the ListBox would show instead. Any time you need to perform another long operation you set the animation control to visible again.
Clean shutdown in Silverlight and WPF applications
Check how the author of this application did it via code maybe it can help you though it is different scenario.

Where does the presentation logic go for the View-Model-ViewModel pattern?

In the View-Model-ViewModel, actions are essentially executed by the viewmodel that is bound to the view. However, where would the "presentation logic" go since the code behind isn't used and the viewmodel has no reference or knowledge of the control that invoked it?
For example, what if I wanted to animate another control when a button is clicked. Would this still go in the code behind?
To expand on Justin Niessner's comment, I'd be using a trigger to achieve this animation as it's all UI bound.
Think about this:
Where would you put code so that when someone mouses over a button it becomes hilighted?
What about code for "depressing" that button when it is clicked?
These and your question are all variations on a theme, so I'd say do it in the GUI.
However, there's one exception to this rule. If the "animation" is a processing animation, then it might be worth tying it to the ViewModel so the ViewModel can control how long the animation runs while it's processing something. Otherwise do it in the GUI.
EDIT: Based on your comment. Ok so the animation should run off a property in the ViewModel, not the button click event. The Click should start the processing in the ViewModel through a command, and the execute code for that command should set a processing flag property on the ViewModel. Then the View can bind to that processing flag and display a progress bar or whatever when that flag is set.

Resources