I have a large WPF application. I'm looking to make the UI multithreaded. As part of that process, I'm moving some application-level control and style resources to the Window level. (I know from experience that I can't use DynamicResource and resolve at the application level unless I'm on the application thread.) Anyway, I moved a bunch of control resources. The application works find with one nasty problem: all of my animations on FrameworkElement Height and Width broke. They all fail because the control is of width or height NaN. These animations all work when the control templates are registered at the application level. All of my controls where I animate height or width have appropriate default height or width values that are not NaN. Why would the resource location affect this?
Asked
Active
Viewed 589 times
1
-
Unfortunately, "large WPF application" isn't enough information for me to reproduce your issue. I've got a very large WPF application in VS right on my desktop here, and it's not displaying the same behavior as yours. Maybe since this is a specific issue with specific XAML in a specific context, you could share some more specifics. – 15ee8f99-57ff-4f92-890c-b56153 Oct 10 '16 at 17:28
-
I would love to post some code with this question, but I can't seem to reproduce the problem in any academic situation. – Brannon Oct 10 '16 at 20:45
-
In my attempts to isolate this, I've posted a related question here: http://stackoverflow.com/questions/39987144/window-level-staticresource-fails-two-levels-deep – Brannon Oct 11 '16 at 21:35
-
Thanks, I'll take a look – 15ee8f99-57ff-4f92-890c-b56153 Oct 11 '16 at 21:40
2 Answers
1
Use ActualWidth and ActualHeight. Nan means that these propeties are not set yet. https://stackoverflow.com/a/607849/3955716
0
This is late, but in case someone else get into this. the DoubleAnimation is a helper class that allows for a double to go from a value to another in a smooth fashion. As you don't specify a Width explicitly for your grid, the default value is NaN. Thus the DoubleAnimation is trying to go from NaN to Whatever the target value. This cannot be done for obvious reasons. If you set the Width of the grid, it should work properly.
A workaround would be to set the grid width after it loads in the constructor :
public Grid()
{
InitializeComponent();
this.Loaded += (s, _) => this.Width = this.ActualWidth;
}
Hope this help.

lindexi
- 4,182
- 3
- 19
- 65

L Chougrani
- 588
- 4
- 7