Topic Author
Posts: 47
Joined: 04 Dec 2014, 14:47

FindName (Dynamic controls)

01 Aug 2015, 21:10

Hi, I have this piece of code
  var newview = (UserControl) NoesisGUISystem.LoadXaml(xaml); 
  newview.Name = "DivView";

  var prueba = DivContent.FindName("DivView");
and "prueba" is null, the object is there if you get it by DivContent.Children[0] but the problem is affecting me when I try to call a Storyboard with this DivView as target,
		<Storyboard x:Key="StoryboardLoadView">
			<ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="DivNewView">
				<EasingThicknessKeyFrame KeyTime="0:0:0.2" Value="0">
						<CircleEase EasingMode="EaseOut"/>

sb.Begin(DivContent) gives just an exception saying "The target name 'DivView' cannot be found in the xaml namscope, and I am afraid that both issues are related.

Btw, I call another storyboard with target declared in xaml and it works perfectly so I think it is something with the controls added dynamically.

User avatar
Site Admin
Posts: 3064
Joined: 22 Dec 2011, 19:20

Re: FindName (Dynamic controls)

03 Aug 2015, 12:16


In order to find a dynamically created element you have to first register it in the tree namescope.

For example, if I loaded a XAML and want to add to the root Grid a Button created in code:
Button button = new Button { Content = "Button" };
I have to register the button in the NameScope:
grid.RegisterName("btn", button);
Since that moment I can do a FindName without problem:
var btn = grid.FindName("btn");

Who is online

Users browsing this forum: Google [Bot] and 3 guests