Genom
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
  DivContent.Children.Clear();
  var newview = (UserControl) NoesisGUISystem.LoadXaml(xaml); 
  newview.Name = "DivView";
  DivContent.Children.Add(newview);

  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,
	<Window.Resources>
		<Storyboard x:Key="StoryboardLoadView">
			<ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="DivNewView">
				<EasingThicknessKeyFrame KeyTime="0:0:0.2" Value="0">
					<EasingThicknessKeyFrame.EasingFunction>
						<CircleEase EasingMode="EaseOut"/>
					</EasingThicknessKeyFrame.EasingFunction>
				</EasingThicknessKeyFrame>
			</ThicknessAnimationUsingKeyFrames>
		</Storyboard>

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.

cheers!
 
User avatar
sfernandez
Site Admin
Posts: 1915
Joined: 22 Dec 2011, 19:20

Re: FindName (Dynamic controls)

03 Aug 2015, 12:16

Hi,

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" };
grid.Children.Add(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: No registered users and 1 guest