2

I'm using a custom control to display news data. I store the control in codebehind so I can display it without having to reload the information (takes anywhere from 2-5 seconds to perform all the tasks associated with populating the control). The news control is essentially a ScrollViewer wrapped in a StackPanel so there's not too much to it. I'm trying to animate the height and width of the control when I call a method, but I'm getting an InvalidOperationException saying

"name cannot be found in the name scope of MainWindow"

I set the name of the control within the MainWindow constructor:

NewsControl _newsControl  = new NewsControl() { Name = "newsControl" };

and I've even tried accessing it when I set the target of my DoubleAnimation using the property value:

Storyboard.SetTargetName(heightAnimation, _newsControl.Name);


As mentioned, the control isn't in the XAML because I need the control in the codebehind anyway...so help?

Nathan Brown
  • 311
  • 1
  • 3
  • 12
  • Don't create or manipulate UI elements in procedural code in WPF. – Federico Berasategui Jul 23 '13 at 20:37
  • `takes anywhere from 2-5 seconds to perform all the tasks associated with populating the control` - UI is not data. Have your data retrieved asynchronously, that has NOTHING to do with UI. – Federico Berasategui Jul 23 '13 at 20:38
  • All the data is retrieved asynchronously, but when a new location is selected (application finds news based on the name of a location) new data must be found, and the control is removed from the screen until the new data is available. I'm trying to create an animation for when the new data is displayed in the new control, sort of like the animation when a minimized window is opened from the taskbar. Sorry for the misleading/incomplete description. – Nathan Brown Jul 23 '13 at 22:33
  • once again, don't manipulate UI elements in procedural code in WPF. If you don't need the control to be shown in the screen at any given moment, create a `public bool ShowControl {get;set;}` property in the ViewModel and have the `` property bound to that in XAML. Then you can have a `DataTrigger` or something fire the animation when that becomes true or what have you. – Federico Berasategui Jul 23 '13 at 22:36
  • For now I'm just looking for functionality, and it's a tad late in the coding process to go back and change these less-than-trivial code structure issues...I really appreciate the feedback though, and these are all things I'll apply in the future. I've only been programming off and on for a couple years now (still a student) so all the knowledge is extremely valuable. – Nathan Brown Jul 26 '13 at 21:18

1 Answers1

3

Since you have a reference to the control, why not use Storyboard.SetTarget instead?

Storyboard.SetTarget(heightAnimation, _newsControl);
McGarnagle
  • 101,349
  • 31
  • 229
  • 260