Wait for all WPF animations to stop - wpf

Given a WPF window, which may or may not have animations, I'd like to wait until they are all done before continuing processing. Is there a generic way to do this. Currently I can do something like this:
void WaitForAnimation(Storyboard storyboard)
{
WaitUntil(() => storyboard.GetCurrentState() == ClockState.Stopped);
}
But this assumes I know the storyboards or have some way of finding them. Is there a way of doing that?

A suggestion is that use Storyboard.Completed event to find out the completion of each story board.

Why not just give your storyboards a x:Name and put them into a collection in codebehind? How many of them have you got?

Related

WPFToolkit / AvalonDock / Event

What I like to have:
A Dashboard which the user can realigned/move/resize a board with Drag&Drop ability.
The easiest solution could be perhabs to use simple dock windows like AvalonDock. But because I don't like floating windows, I must be possible to automatic re-dock the floating window if the user don't dock it.
What I tried:
I found in LayoutContent.Dock the method, which I need to call when the user stops dragging the floating window. I think, after LayoutFloatingWindowControl.FilterMessage -> case Win32Helper.WM_EXITSIZEMOVE: would be the best place. But after this point I found no event to notify me. Do I didn't see the solution? Next problem: How can I get the object of type LayoutContent to call Dock? :/
Can I continue to pursue my idea?
Or did you know other free controls or ideas to realize this?
Thanks
AvalonDock LayoutDocument and LayoutAnchorable (the controls that hold the content) have a property CanFloatset it to false and it will not be able to float and you don't need to dock it.
see the docs: https://github.com/xceedsoftware/wpftoolkit/wiki/AvalonDock

Starting a Storyboard From A Storyboard

Is this possible in expression blend?
What i am looking for is to start a storyboard, then at second 2 i would like to start another storyboard.
Why am I looking for this? So I can re-use storyboards and 'pieces' of larger storyboards. Seems like it would be the most OO I can get from storyboards.
Thanks!
Storyboards are not supposed to be OO, they are animation and timeline tools. If you want to create custom and controlled animations, I would advice that you research about the CompositeTarget.Rendering event and read about animations and easing. You can use IEasingFunction to select the easing your custom animations would use.
A different option would be to use the Storyboard.Completed event to find out when a storyboard is finished so that you can start another one, but if you decide to go this way you have to be very careful about memory leaks related with event hooks.

WPF: Is there a way to directly get the Window object a control is on?

Is there a way to directly get the Window object a control is on?
Assuming the Control is directly below the Window, you can call GetParent on the LogicalTreeHelper.
You may, of course, need to Use the LogicalTreeHelper to traverse the tree if the Control is not a direct descendant.
Most likely you are on the applications active window in the moment you interact with the control.
So in that case, this other post on stackoverflow could be helpful too:
stackoverflow: get active window(s) in wpf
A bit late, but the following works pretty fine for me.
var ownerWindow = Window.GetWindow(yourControl);

What is the way to minimize number of similar event handlers?

A WPF window should have hundreds of objects (rows of rectangles) and mouse clicking on each of them should fire similar actions. These actions differ only by several parameters (say "No. of Row" and "No. of position in a row").
Should I have hundreds of almost the same event handlers or how I could optimize my code?
Please give me some tips, just to move to the right direction.
Best regards.
WPF mitigates this problem by introducing Routed Events. At any level in the element hierarchy you may intercept events from its child elements and base your logic depending on the actual element that received this event in the first place (as presented by the Source property of RoutedEventArgs).
I'm no expert in WPF, but in event handling you could write 1 dans point every similar event to this handler. In the handler use the senter parameter to know whish control it came from.
Instead of 100's of similar event hander you could have a big one with a switch
Hope that's help
Can't you just use an instance of ICommand on your viewmodel and use the command parameter to determine which rectangle was clicked?

How can I speed up the rendering of my WPF ListBox?

I have a WPF ListBox control (view code) and I am keeping maybe like 100-200 items in it. Every time the ObservableCollection it is bound to changes though it takes it a split second to update and it freezes the whole UI. Is there a way to add elements incrementally or something I can do to improve the performance of this control?
Try something where (PanelList is a ListBox or something);
new Task(delegate {
foreach (var info in new DirectoryInfo("C:\\windows\\system32").EnumerateFiles()) {
PanelList.Dispatcher.Invoke(DispatcherPriority.Background, (Action)delegate {
PanelList.Items.Add(info);
});
Thread.Sleep(0);
}
}).Start();
You want to run a background task and update a UI control incrementally through Dispatcher.Invoke, make sure to set your priority relativly low, and I always throw a sleep in just for fun (voluntarially context swap), also you should be checking if your current task has been canceled...
Oh ya, this is not so much a performance improvement as precieved performance and UI responsiveness.
Try setting VirtualizingStackPanel.IsVirtualizing="True" on your ListBox - MSDN Documentation. Also see this blog post I came across. I haven't tried it personally, but it seems like a good place to start. Good luck!

Resources