How to get x:Name from 3DElements using XamlReader.Load<p>I would like to get all Names from DependencyObject- Elements in my Viewport3D. As we know: none of the 3D Elements has a Name Property, so x:Name is used for instance description. <br/>How can I resolve the x:Name for every Element in my 3D tree?<br/><br/>A nice Trick to locate the name of an 3DElement is to get all Fields that where generated for the x:Names:<br/><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas">FieldInfo[] fieldInfos = rootElement.GetType().GetFields(</span></span></span><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas"> BindingFlags.Public</span></span></span><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas"> | BindingFlags.NonPublic</span></span></span><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas"> | BindingFlags.Instance</span></span></span><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas">);<br/></span></span></span>And then compare the object thats given back from FindName(fieldInfo.Name) with all 3DElements in tree. Something like this:<br/><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas">foreach (FieldInfo info in fieldInfos)<br/></span></span></span><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas">{<br/></span></span></span><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas">  if(compareDepObj == FindName(info.Name))<br/>     compareDepObjHasNameInfoName <br/><br/></span></span></span><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas">}<br/></span></span></span><br/>This works fine when I load a compiled UserControl assembly. But when I use XamlReader.Load/Parse I could not find any of the x:Name Fields. <br/>I think they must be there because a FindName(X:NAME) always works. And Documentation for x:Name says:<br/><span><span class=input><span style="color:#0000ff">&quot;x:Name</span></span></span> is used by a XAML processor to register a name into a namescope, even for cases where the page is not compiled (for instance, loose XAML).&quot;<br/>Do I use the wrong BindingFlags? <br/>Has anybody an Idea?<br/><br/><br/></p><hr class="sig">ooK© 2009 Microsoft Corporation. All rights reserved.Tue, 20 Oct 2009 23:38:13 Z5c226430-c54d-45b8-a8a2-7e4a79e3692ahttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5c226430-c54d-45b8-a8a2-7e4a79e3692a#5c226430-c54d-45b8-a8a2-7e4a79e3692ahttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5c226430-c54d-45b8-a8a2-7e4a79e3692a#5c226430-c54d-45b8-a8a2-7e4a79e3692amatzeooKhttp://social.msdn.microsoft.com/Profile/en-US/?user=matzeooKHow to get x:Name from 3DElements using XamlReader.Load<p>I would like to get all Names from DependencyObject- Elements in my Viewport3D. As we know: none of the 3D Elements has a Name Property, so x:Name is used for instance description. <br/>How can I resolve the x:Name for every Element in my 3D tree?<br/><br/>A nice Trick to locate the name of an 3DElement is to get all Fields that where generated for the x:Names:<br/><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas">FieldInfo[] fieldInfos = rootElement.GetType().GetFields(</span></span></span><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas"> BindingFlags.Public</span></span></span><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas"> | BindingFlags.NonPublic</span></span></span><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas"> | BindingFlags.Instance</span></span></span><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas">);<br/></span></span></span>And then compare the object thats given back from FindName(fieldInfo.Name) with all 3DElements in tree. Something like this:<br/><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas">foreach (FieldInfo info in fieldInfos)<br/></span></span></span><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas">{<br/></span></span></span><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas">  if(compareDepObj == FindName(info.Name))<br/>     compareDepObjHasNameInfoName <br/><br/></span></span></span><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas"><span style="font-size:x-small;color:#008000;font-family:Consolas">}<br/></span></span></span><br/>This works fine when I load a compiled UserControl assembly. But when I use XamlReader.Load/Parse I could not find any of the x:Name Fields. <br/>I think they must be there because a FindName(X:NAME) always works. And Documentation for x:Name says:<br/><span><span class=input><span style="color:#0000ff">&quot;x:Name</span></span></span> is used by a XAML processor to register a name into a namescope, even for cases where the page is not compiled (for instance, loose XAML).&quot;<br/>Do I use the wrong BindingFlags? <br/>Has anybody an Idea?<br/><br/><br/></p><hr class="sig">ooKSat, 13 Jun 2009 13:12:55 Z2009-06-13T13:12:55Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5c226430-c54d-45b8-a8a2-7e4a79e3692a#9754255c-56e3-47bd-99e2-73fd2c692aa5http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5c226430-c54d-45b8-a8a2-7e4a79e3692a#9754255c-56e3-47bd-99e2-73fd2c692aa5Rob Relyeahttp://social.msdn.microsoft.com/Profile/en-US/?user=Rob%20RelyeaHow to get x:Name from 3DElements using XamlReader.LoadCompiled XAML or XAML will attempt to register all x:Names (or Names on classes which have declared it their special name property via [RuntimeNamePropertyAttribute] (like FrameworkElement does) with the nearest namescope.<br/>Compilation of XAML creates the fields.<br/><br/>So what you are seeing is expected.  When you use XamlReader.Load, there are no fields, since you didn't compile the XAML. However, findName still works.<br/><br/>One solution for this is to make your root element implement INameScope.  You can then iterate through the list, since you own it.<br/><br/>In .NET 4, your scenario could get a bit easier, as you could write a custom XAML node loop and understand all names and the objects they map to.  Perhaps I should write a quick sample for that...<br/><br/>Thanks, Rob Relyea<br/>WPF/XAML Team<br/><a href="http://robrelyea.com/blog">http://robrelyea.com/blog</a>Fri, 19 Jun 2009 10:20:50 Z2009-06-19T10:20:50Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5c226430-c54d-45b8-a8a2-7e4a79e3692a#cf3e1ae7-afec-46ee-bd90-66f0b351246chttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5c226430-c54d-45b8-a8a2-7e4a79e3692a#cf3e1ae7-afec-46ee-bd90-66f0b351246cRob Relyeahttp://social.msdn.microsoft.com/Profile/en-US/?user=Rob%20RelyeaHow to get x:Name from 3DElements using XamlReader.LoadI've posted a sample using .NET 4 beta 1 on my blog: &quot;<a href="http://blogs.windowsclient.net/rob_relyea/archive/2009/06/22/xaml-node-loop-example-finding-all-named-object-instances.aspx">XAML node loop example: finding all named object instances</a>&quot;<br/><br/>Thanks, RobTue, 23 Jun 2009 11:30:57 Z2009-06-23T11:30:57Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5c226430-c54d-45b8-a8a2-7e4a79e3692a#49604890-1409-4810-8344-73b57170bcaahttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5c226430-c54d-45b8-a8a2-7e4a79e3692a#49604890-1409-4810-8344-73b57170bcaamatzeooKhttp://social.msdn.microsoft.com/Profile/en-US/?user=matzeooKHow to get x:Name from 3DElements using XamlReader.Loadthank you Rob!<br/>Until I can use .NET 4 Release I search all my x:Names using XML SelectNode.<br/>Thank you for the nice sample in your Blog.<hr class="sig">ooKMon, 29 Jun 2009 09:50:48 Z2009-06-29T09:50:48Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5c226430-c54d-45b8-a8a2-7e4a79e3692a#074832a1-0256-4727-9674-c86ba2d7fcf3http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5c226430-c54d-45b8-a8a2-7e4a79e3692a#074832a1-0256-4727-9674-c86ba2d7fcf3EeeeEfffhttp://social.msdn.microsoft.com/Profile/en-US/?user=EeeeEfffHow to get x:Name from 3DElements using XamlReader.Load<div style="outline-width: 0px; outline-style: initial; outline-color: initial; font-weight: inherit; font-style: inherit; font-family: inherit; padding: 0px; margin: 0px; border: 0px initial initial;"><span style="font-family: Calibri, sans-serif; font-size: small;"><span style="font-size: 13px;">I'm glad I came across this post, as I've been trying to do the same thing and bumping against this problem for a few days now. &nbsp;</span></span></div> <div style="outline-width: 0px; outline-style: initial; outline-color: initial; font-weight: inherit; font-style: inherit; font-family: inherit; padding: 0px; margin: 0px; border: 0px initial initial;"><span style="font-family: Calibri, sans-serif; font-size: small;"><span style="font-size: 13px;"><br /></span></span></div> <div style="outline-width: 0px; outline-style: initial; outline-color: initial; font-weight: inherit; font-style: inherit; font-family: inherit; padding: 0px; margin: 0px; border: 0px initial initial;"><span style="font-family: Calibri, sans-serif; font-size: small;"><span style="font-size: 13px;">I like the .Net 4.0 solution, but it is not really something I can use at this point in time. &nbsp;So, in order for me to build a TreeView of my XAML 3D model, do I really have to resort to reading the XAML a second time, searching for&nbsp;<span style="outline-width: 0px; outline-style: initial; outline-color: initial; font-weight: inherit; font-style: inherit; font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; padding: 0px; margin: 0px; border: 0px initial initial;">x:Names using XML SelectNode? &nbsp;Does not sound like the right way to go - some of my XAML model files are 300+MB...</span></span></span></div> <div style="outline-width: 0px; outline-style: initial; outline-color: initial; font-weight: inherit; font-style: inherit; font-family: inherit; padding: 0px; margin: 0px; border: 0px initial initial;"><br /></div> <div style="outline-width: 0px; outline-style: initial; outline-color: initial; font-weight: inherit; font-style: inherit; font-family: inherit; padding: 0px; margin: 0px; border: 0px initial initial;">You mentioned "<em>One solution for this is to make your root element implement INameScope.&nbsp; You can then iterate through the list, since you own it.</em>" &nbsp;I'm not sure I understand how this would work. &nbsp;My root element is the Window which contains the Viewport3D. &nbsp;Are you suggesting to implement INameScope in the Window, and call INameScope.RegisterName to&nbsp;<span style="font-family: Arial, Helvetica, sans-serif; font-size: 21px; font-weight: bold;"><span style="font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 11px; font-weight: normal;">add each Visual3D elements to the Window's namescope? &nbsp;And that would somehow allow me to get the&nbsp;Visual3D&nbsp;element's name?</span></span></div> <div style="outline-width: 0px; outline-style: initial; outline-color: initial; font-weight: inherit; font-style: inherit; font-family: inherit; padding: 0px; margin: 0px; border: 0px initial initial;"><br /></div> <div style="outline-width: 0px; outline-style: initial; outline-color: initial; font-weight: inherit; font-style: inherit; font-family: inherit; padding: 0px; margin: 0px; border: 0px initial initial;">How does Expression Blend display the tree (including the element names) when a XAML 3D file is loaded? &nbsp;Does it compile it? &nbsp;Is there some way for me to compile my XAML file as I'm importing it, in order to get the field names??</div> <div style="outline-width: 0px; outline-style: initial; outline-color: initial; font-weight: inherit; font-style: inherit; font-family: inherit; padding: 0px; margin: 0px; border: 0px initial initial;"><br /></div> <div style="outline-width: 0px; outline-style: initial; outline-color: initial; font-weight: inherit; font-style: inherit; font-family: inherit; padding: 0px; margin: 0px; border: 0px initial initial;">Thanks for any insight.</div> <div style="outline-width: 0px; outline-style: initial; outline-color: initial; font-weight: inherit; font-style: inherit; font-family: inherit; padding: 0px; margin: 0px; border: 0px initial initial;">E.</div> <div style="outline-width: 0px; outline-style: initial; outline-color: initial; font-weight: inherit; font-style: inherit; font-family: inherit; padding: 0px; margin: 0px; border: 0px initial initial;"><br /></div>Thu, 15 Oct 2009 16:38:04 Z2009-10-15T16:38:04Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5c226430-c54d-45b8-a8a2-7e4a79e3692a#0a089547-c176-4ded-a69c-97865148dfb9http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5c226430-c54d-45b8-a8a2-7e4a79e3692a#0a089547-c176-4ded-a69c-97865148dfb9Rob Relyeahttp://social.msdn.microsoft.com/Profile/en-US/?user=Rob%20RelyeaHow to get x:Name from 3DElements using XamlReader.LoadE.-<br />If you built a MyWindow class:<br /><br />public class MyWindow : Window, INameScope<br />{<br />...<br />}<br /><br />&lt;l:MyWindow x:Class="MyWindow1"&gt;<br />...<br />&lt;/l:MyWindow&gt;<br /><br />In .Net 3 and later, the XAML parser will call INameScope.RegisterName passing in the name and object instance.&nbsp; So this, if it is an option, is an elegant way to do it that works in v3 and v4.<br /><br />Blend displays element names because they effectively have their own XAML parser, and can see the names before they are created...easily being able to coorelate the names to the eventual instances...<br /><br />Hope that helps.<br />-Rob<br /><br />Fri, 16 Oct 2009 16:50:24 Z2009-10-16T16:50:24Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5c226430-c54d-45b8-a8a2-7e4a79e3692a#baf9258e-8ade-4d41-8642-971c4c6b1f26http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5c226430-c54d-45b8-a8a2-7e4a79e3692a#baf9258e-8ade-4d41-8642-971c4c6b1f26EeeeEfffhttp://social.msdn.microsoft.com/Profile/en-US/?user=EeeeEfffHow to get x:Name from 3DElements using XamlReader.LoadThanks so much for responding, Rob! <div><br/></div> <div>Based on your suggestion, here is what I did:</div> <div>In my window base class, WindowBase, I implemented INameScope (implementation below).  I had to implement a helper method (my own GetNameScope), because by default, I was getting null when I called <span style="font-family:monospace;white-space:pre">NameScope.GetNameScope(this);</span></div> <div><br/></div> <div> <pre lang="x-c#"> #region INameScope Members object System.Windows.Markup.INameScope.FindName(string name) { INameScope scope = GetNameScope(); return null != scope ? scope.FindName(name) : null; } void System.Windows.Markup.INameScope.RegisterName(string name, object scopedElement) { INameScope scope = GetNameScope(); if (null != scope) scope.RegisterName(name, scopedElement); } void System.Windows.Markup.INameScope.UnregisterName(string name) { INameScope scope = GetNameScope(); if (null != scope) scope.UnregisterName(name); } #endregion #region GetNameScope_Helper protected INameScope GetNameScope() { //return NameScope.GetNameScope(this); // returns null, unless I create my own...? DependencyObject element = this; while (null != element) { INameScope nameScope = NameScope.GetNameScope(element); if (null != nameScope) return nameScope; element = LogicalTreeHelper.GetParent(element) ?? VisualTreeHelper.GetParent(element); } if (savedScope == null) { savedScope = new NameScope(); } return savedScope; } #endregion // GetNameScope_Helper </pre> <br/></div> <div>Then, once I imported my XAML 3D file, to register, say my viewport object, I would do the following:</div> <div> <div><span style="white-space:pre"> </span>NameScope.SetNameScope(view3D, GetNameScope());</div> <div>But that wouldn't add the view3D to the namescope (the default namescope was null, and even when I created my own namescope, view3D was not being added).  So I added the following call to do so:</div> <div>        RegisterName(view3D.Name, view3D);</div> <div><br/></div> <div>That added the the view3D object to the namescope, but that kinda defeats the purpose, as I will <em>not</em> have the names of all the elements a priori to add them explicitely.  Am I missing something?</div> <div><br/></div> <div>Thanks so much!</div> <div>E.</div> <div><br/></div> <div><br/></div> </div> <div><br/></div>Mon, 19 Oct 2009 15:09:33 Z2009-10-20T00:35:27Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5c226430-c54d-45b8-a8a2-7e4a79e3692a#54b55266-2d8e-469e-90b4-407c09a4f910http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5c226430-c54d-45b8-a8a2-7e4a79e3692a#54b55266-2d8e-469e-90b4-407c09a4f910Rob Relyeahttp://social.msdn.microsoft.com/Profile/en-US/?user=Rob%20RelyeaHow to get x:Name from 3DElements using XamlReader.Load<p>E.-<br/>Try this:<br/><br/>using System;<br/>using System.Collections.Generic;<br/>using System.Text;<br/>using System.Windows;<br/>using System.Windows.Markup;</p> <p>namespace WindowWithNameScope<br/>{<br/>    //This class, if placed at the root of a XAML file which is loaded by XamlReader.Load()<br/>    //will end up having all named elements contained in its nameTable automatically...<br/>    //<br/>    //If you want to get that list, it is now in your power.<br/>    public class WindowWithNameScope : Window, INameScope<br/>    {<br/>        <br/>        Dictionary&lt;string, object&gt; nameTable = new Dictionary&lt;string, object&gt;();</p> <p>        object INameScope.FindName(string name)<br/>        {<br/>            object element = null;<br/>            nameTable.TryGetValue(name, out element);<br/>            return element;<br/>        }</p> <p>        void INameScope.RegisterName(string name, object scopedElement)<br/>        {<br/>            nameTable[name] = scopedElement;<br/>        }</p> <p>        void INameScope.UnregisterName(string name)<br/>        {<br/>            nameTable[name] = null;<br/>        }<br/>    }<br/>}<br/><br/>Hope that helps... (I didn't test this, let me know if it doesn't work, and I'll go do that...)<br/>Thanks, Rob</p>Tue, 20 Oct 2009 00:55:24 Z2009-10-20T00:55:24Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5c226430-c54d-45b8-a8a2-7e4a79e3692a#243bc66a-3655-4272-8887-41982f4a5ab7http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5c226430-c54d-45b8-a8a2-7e4a79e3692a#243bc66a-3655-4272-8887-41982f4a5ab7EeeeEfffhttp://social.msdn.microsoft.com/Profile/en-US/?user=EeeeEfffHow to get x:Name from 3DElements using XamlReader.LoadHi again, Rob; thanks for the suggestion. <div><br/></div> <div>I tried your setup above: </div> <div><span style="font-family:'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;font-size:13px;color:#333333;line-height:16px">  public class WindowWithNameScope : Window, INameScope {</span></div> <div><span style="font-family:'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;color:#333333;font-size:small"><span style="font-size:13px;line-height:16px">    ...</span></span></div> <div><span style="font-family:'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;color:#333333;font-size:small"><span style="font-size:13px;line-height:16px">  }</span></span></div> <div><span style="font-family:'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;color:#333333;font-size:small"><span style="font-size:13px;line-height:16px"><br/></span></span></div> <div><span style="font-family:'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;color:#333333;font-size:small"><span style="font-size:13px;line-height:16px">Then, I created a Window1.xaml + cs: </span></span></div> <div><span style="font-family:'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;color:#333333;font-size:small"><span style="font-size:13px;line-height:16px">.XAML:</span></span></div> <div><span style="font-family:'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;color:#333333;font-size:small"><span style="font-size:13px;line-height:16px">  &lt;LoadXAML3D:WindowWithNameScope x:Class=&quot;LoadXAML3D.Window1&quot;</span></span></div> <div><span style="font-family:'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;color:#333333;font-size:small"><span style="font-size:13px;line-height:16px">  ...</span></span></div> <div><span style="font-family:'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;color:#333333;font-size:small"><span style="font-size:13px;line-height:16px"> <div>  &lt;/LoadXAML3D:WindowWithNameScope&gt;</div> <div>&amp; .cs</div> <div>  public partial class Window1 : WindowWithNameScope {</div> <div>    ...</div> <div>  }</div> <div><br/></div> <div>Window1 has a browse button:</div> <div>  &lt;Button Width=&quot;65&quot; Content=&quot;Browse&quot; VerticalAlignment=&quot;Center&quot; Margin=&quot;4,0,0,0&quot; Click=&quot;Browse_Click&quot;/&gt;</div> </span></span></div> <div><span style="font-family:'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;color:#333333;font-size:small"><span style="font-size:13px;line-height:16px"><br/></span></span> <div>In the button event handler, I loaded the XAML file (bottom of this message) as follows, and added the resulting Viewport3D to a Border.Child in Window1:</div> <div> <div>            FileStream fs = File.OpenRead(fileName);</div> <div>            Viewport3D view3D = (Viewport3D)System.Windows.Markup.XamlReader.Load(fs);</div> </div> <div><br/></div> <div>The viewport displays the model, but none of the named entities in the XAML file make it into the nameTable (or trigger the RegisterName method).</div> <div><br/></div> <div>Your comment in the previous post says: &quot;<span style="font-family:'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;font-size:13px;color:#333333;line-height:16px">This class, if placed at the root of a XAML file which is loaded by XamlReader.Load()...<span style="color:#000000;font-family:Verdana, Arial, Helvetica, sans-serif;font-size:11px;line-height:normal">&quot;</span></span></div> <div>Do you mean that the XAML file that I am dynamically loading should have &quot;<span style="font-family:'Segoe UI', 'Lucida Grande', Verdana, Arial, Helvetica, sans-serif;font-size:13px;color:#333333;line-height:16px">WindowWithNameScope<span style="color:#000000;font-family:Verdana, Arial, Helvetica, sans-serif;font-size:11px;line-height:normal">&quot; as the root of that document, in order for this setup to work?</span></span></div> <div><br/></div> <div>Thanks so much.</div> <div>E.</div> <div><br/></div> <div>Sample (abbreviated) 3D file: none of the named elements are added to the namescope.</div> <div><br/></div> <div> <pre lang=x-xml>&lt;Viewport3D x:Name=&quot;RootViewport3D&quot; xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot; xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot; xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot; xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot; mc:Ignorable=&quot;d&quot;&gt; &lt;Viewport3D.Camera&gt; &lt;PerspectiveCamera FieldOfView=&quot;45&quot; FarPlaneDistance=&quot;452.66506021275518&quot; LookDirection=&quot;25.151,-7.26,-12.543&quot; NearPlaneDistance=&quot;0.1&quot; Position=&quot;-25.151,8.363,12.543&quot; UpDirection=&quot;0.226,0.969,-0.108&quot;/&gt; &lt;/Viewport3D.Camera&gt; &lt;ModelVisual3D x:Name=&quot;World&quot;&gt; &lt;ModelVisual3D.Transform&gt; &lt;Transform3DGroup&gt; &lt;TranslateTransform3D OffsetX=&quot;0&quot; OffsetY=&quot;0&quot; OffsetZ=&quot;0&quot;/&gt; &lt;ScaleTransform3D ScaleX=&quot;1&quot; ScaleY=&quot;1&quot; ScaleZ=&quot;1&quot;/&gt; &lt;RotateTransform3D d:EulerAngles=&quot;0,0,0&quot;&gt; &lt;RotateTransform3D.Rotation&gt; &lt;AxisAngleRotation3D Angle=&quot;0&quot; Axis=&quot;0,1,0&quot;/&gt; &lt;/RotateTransform3D.Rotation&gt; &lt;/RotateTransform3D&gt; &lt;TranslateTransform3D OffsetX=&quot;0&quot; OffsetY=&quot;0&quot; OffsetZ=&quot;0&quot;/&gt; &lt;TranslateTransform3D OffsetY=&quot;2.266&quot; OffsetZ=&quot;0.319&quot;/&gt; &lt;/Transform3DGroup&gt; &lt;/ModelVisual3D.Transform&gt; &lt;ModelVisual3D x:Name=&quot;AmbientLightContainer&quot;&gt; &lt;ModelVisual3D.Content&gt; &lt;AmbientLight x:Name=&quot;AmbientLight&quot; Color=&quot;#FF7F7F7F&quot;/&gt; &lt;/ModelVisual3D.Content&gt; &lt;/ModelVisual3D&gt; &lt;ModelVisual3D x:Name=&quot;DirectionalLightContainer&quot;&gt; &lt;ModelVisual3D.Content&gt; &lt;DirectionalLight x:Name=&quot;DirectionalLight&quot; Color=&quot;#FF3F3F3F&quot; Direction=&quot;0,0,-1&quot;&gt; &lt;DirectionalLight.Transform&gt; &lt;TranslateTransform3D OffsetX=&quot;0&quot; OffsetY=&quot;0&quot; OffsetZ=&quot;3&quot;/&gt; &lt;/DirectionalLight.Transform&gt; &lt;/DirectionalLight&gt; &lt;/ModelVisual3D.Content&gt; &lt;/ModelVisual3D&gt; &lt;ModelVisual3D x:Name=&quot;EntirePen&quot;&gt; &lt;ModelVisual3D x:Name=&quot;Pen&quot;&gt; &lt;ModelVisual3D x:Name=&quot;PenTip&quot;&gt; &lt;ModelVisual3D.Transform&gt; &lt;Transform3DGroup&gt; &lt;TranslateTransform3D OffsetX=&quot;0&quot; OffsetY=&quot;0&quot; OffsetZ=&quot;0&quot;/&gt; &lt;ScaleTransform3D ScaleX=&quot;1&quot; ScaleY=&quot;1&quot; ScaleZ=&quot;1&quot;/&gt; &lt;RotateTransform3D d:EulerAngles=&quot;0,0,0&quot;&gt; &lt;RotateTransform3D.Rotation&gt; &lt;AxisAngleRotation3D Angle=&quot;0&quot; Axis=&quot;0,1,0&quot;/&gt; &lt;/RotateTransform3D.Rotation&gt; &lt;/RotateTransform3D&gt; &lt;TranslateTransform3D OffsetX=&quot;0&quot; OffsetY=&quot;0&quot; OffsetZ=&quot;0&quot;/&gt; &lt;TranslateTransform3D OffsetX=&quot;0&quot; OffsetY=&quot;0&quot; OffsetZ=&quot;0&quot;/&gt; &lt;/Transform3DGroup&gt; &lt;/ModelVisual3D.Transform&gt; &lt;ModelVisual3D x:Name=&quot;Tip_FeltContainer&quot;&gt; &lt;ModelVisual3D.Content&gt; &lt;GeometryModel3D x:Name=&quot;Tip_Felt&quot; d:Bounds=&quot;-14.3089838027954,1.01379096508026,-0.0882589966058731,0.295544624328613,0.176519989967346,0.176519997417927&quot;&gt; &lt;GeometryModel3D.Material&gt; &lt;MaterialGroup&gt; &lt;EmissiveMaterial Brush=&quot;Black&quot;/&gt; &lt;DiffuseMaterial Brush=&quot;Black&quot;/&gt; &lt;SpecularMaterial Brush=&quot;sc#1, 0.25, 0.25, 0.25&quot; SpecularPower=&quot;17.647059&quot;/&gt; &lt;/MaterialGroup&gt; &lt;/GeometryModel3D.Material&gt; &lt;GeometryModel3D.Geometry&gt; &lt;MeshGeometry3D Normals=&quot;0,0.707107,-0.70710657 0,0.707107,-0.70710657 0,0.96592642,-0.25881682 0,0.96592642,-0.25881682 0,0.96592642,0.25881681 0,0.96592642,0.25881681 0,0.70710702,0.70710655 0,0.70710702,0.70710655 0,0.25881722,0.96592632 0,0.25881722,0.96592632 0,-0.25881689,0.9659264 0,-0.25881689,0.9659264 0,-0.70710636,0.7071072 0,-0.70710636,0.7071072 0,-0.96592716,0.25881408 0,-0.96592716,0.25881408 0,-0.96592642,-0.25881682 0,-0.96592642,-0.25881682 0,-0.70710087,-0.70711269 0,-0.70710087,-0.70711269 0,-0.25881689,-0.9659264 0,-0.25881689,-0.9659264 0,0.25881722,-0.96592632 0,0.25881722,-0.96592632 -0.23420937,0.68743936,-0.68743952 -0.23420771,0.93906062,-0.25161855 -0.23420725,0.93906073,0.25161857 -0.23420867,0.6874395,0.68743962 -0.23420842,0.25161848,0.93906047 -0.23420787,-0.25161843,0.93906062 -0.23420923,-0.68743924,0.68743969 -0.23420746,-0.93906116,0.25161677 -0.23421154,-0.93905973,-0.25161831 -0.23421343,-0.68743492,-0.68744258 -0.23420841,-0.25161838,-0.93906049 -0.23420865,0.25161845,-0.93906042 0,0.707107,-0.70710657 0,0.96592642,-0.25881682 0,0.96592642,0.25881681 0,0.70710702,0.70710655 0,0.25881722,0.96592632 0,-0.25881689,0.9659264 0,-0.70710636,0.7071072 0,-0.96592716,0.25881408 0,-0.96592642,-0.25881682 0,-0.70710087,-0.70711269 0,-0.25881689,-0.9659264 0,0.25881722,-0.96592632 -0.98171005,0.18389522,-0.049273936 -0.98171022,0.13461996,-0.13461984 -1,0,0 -0.9817101,0.18389452,0.049275618 -0.98170991,0.1346176,0.13462447 -0.98171005,0.049274041,0.1838952 -0.98171009,-0.049273991,0.18389501 -0.98171024,-0.13461641,0.13462328 -0.98171039,-0.18389307,0.049275231 -0.9817101,-0.18389499,-0.049273872 -0.98171025,-0.13461986,-0.13461974 -0.98171005,-0.049274043,-0.1838952 -0.98171007,0.049274014,-0.1838951&quot; Positions=&quot;-14.094623,1.166662,-0.064609997 -14.22631,1.166662,-0.064609997 -14.22631,1.190311,-0.023647999 -14.094623,1.190311,-0.023647999 -14.22631,1.190311,0.02365 -14.094623,1.190311,0.02365 -14.22631,1.166662,0.064612001 -14.094623,1.166662,0.064612001 -14.22631,1.1257,0.088261001 -14.094623,1.1257,0.088261001 -14.22631,1.078402,0.088261001 -14.094623,1.078402,0.088261001 -14.22631,1.0374399,0.064612001 -14.094623,1.0374399,0.064612001 -14.22631,1.013791,0.02365 -14.094623,1.013791,0.02365 -14.22631,1.013791,-0.023649 -14.094623,1.013791,-0.023649 -14.22631,1.0374399,-0.064609997 -14.094623,1.0374399,-0.064609997 -14.22631,1.078402,-0.088258997 -14.094623,1.078402,-0.088258997 -14.22631,1.1257,-0.088258997 -14.094623,1.1257,-0.088258997 -14.268888,1.166662,-0.064609997 -14.268888,1.190311,-0.023647999 -14.268888,1.190311,0.02365 -14.268888,1.166662,0.064612001 -14.268888,1.1257,0.088261001 -14.268888,1.078402,0.088261001 -14.268888,1.0374399,0.064612001 -14.268888,1.013791,0.02365 -14.268888,1.013791,-0.023649 -14.268888,1.0374399,-0.064609997 -14.268888,1.078402,-0.088258997 -14.268888,1.1257,-0.088258997 -14.013439,1.166662,-0.064609997 -14.013439,1.190311,-0.023647999 -14.013439,1.190311,0.02365 -14.013439,1.166662,0.064612001 -14.013439,1.1257,0.088261001 -14.013439,1.078402,0.088261001 -14.013439,1.0374399,0.064612001 -14.013439,1.013791,0.02365 -14.013439,1.013791,-0.023649 -14.013439,1.0374399,-0.064609997 -14.013439,1.078402,-0.088258997 -14.013439,1.1257,-0.088258997 -14.308984,1.128366,-0.0070500001 -14.308984,1.121315,-0.019262999 -14.308984,1.102051,1E-06 -14.308984,1.128366,0.0070520001 -14.308984,1.121315,0.019264 -14.308984,1.109102,0.026315 -14.308984,1.095,0.026315 -14.308984,1.082787,0.019264 -14.308984,1.075736,0.0070520001 -14.308984,1.075736,-0.0070500001 -14.308984,1.082787,-0.019262999 -14.308984,1.095,-0.026314 -14.308984,1.109102,-0.026314&quot; TextureCoordinates=&quot;0.70045998,0.30637712 0.30295791,0.30465216 0.30295735,0.21179203 0.70046055,0.2148991 0.30295735,0.21179203 0.70046055,0.2148991 0.30295791,0.30465216 0.70045998,0.30637712 0.30295737,0.43239571 0.70046053,0.43292465 0.30295732,0.56760396 0.70046058,0.56707502 0.30295791,0.69534784 0.70045998,0.69362288 0.30295735,0.78820797 0.70046055,0.7851009 0.30295777,0.7882072 0.70046012,0.78510016 0.30295791,0.69534784 0.70045998,0.69362288 0.30295732,0.56760396 0.70046058,0.56707502 0.30295737,0.43239571 0.70046053,0.43292465 0.22398695,0.34873939 0.22398636,0.2851084 0.22398635,0.2851084 0.22398695,0.34873939 0.22398638,0.4464432 0.22398632,0.55355653 0.22398695,0.65126061 0.22398635,0.7148916 0.2239868,0.71489129 0.22398695,0.65126061 0.22398632,0.55355653 0.22398638,0.4464432 0.82372097,0.37871262 0.82372151,0.33038536 0.82372151,0.33038536 0.82372097,0.37871262 0.82372149,0.45653769 0.82372154,0.5434621 0.82372097,0.62128738 0.82372151,0.66961464 0.8237211,0.66961449 0.82372097,0.62128738 0.82372154,0.5434621 0.82372149,0.45653769 0.058031992,0.44396641 0.058032277,0.45907926 1.2850448E-07,0.49999987 0.058031989,0.44396641 0.058030798,0.45907923 0.058029934,0.48505785 0.05802987,0.5149419 0.058030622,0.54092052 0.058031749,0.55603334 0.058031752,0.55603334 0.058032101,0.54092049 0.05803189,0.51494188 0.058031954,0.48505787&quot; TriangleIndices=&quot;0 1 2 0 2 3 3 2 4 3 4 5 5 4 6 5 6 7 7 6 8 7 8 9 9 8 10 9 10 11 11 10 12 11 12 13 13 12 14 13 14 15 15 14 16 15 16 17 17 16 18 17 18 19 19 18 20 19 20 21 21 20 22 21 22 23 23 22 1 23 1 0 1 24 25 1 25 2 2 25 26 2 26 4 4 26 27 4 27 6 6 27 28 6 28 8 8 28 29 8 29 10 10 29 30 10 30 12 12 30 31 12 31 14 14 31 32 14 32 16 16 32 33 16 33 18 18 33 34 18 34 20 20 34 35 20 35 22 22 35 24 22 24 1 36 0 3 36 3 37 37 3 5 37 5 38 38 5 7 38 7 39 39 7 9 39 9 40 40 9 11 40 11 41 41 11 13 41 13 42 42 13 15 42 15 43 43 15 17 43 17 44 44 17 19 44 19 45 45 19 21 45 21 46 46 21 23 46 23 47 47 23 0 47 0 36 48 49 50 51 48 50 52 51 50 53 52 50 54 53 50 55 54 50 56 55 50 57 56 50 58 57 50 59 58 50 60 59 50 49 60 50&quot;/&gt; &lt;/GeometryModel3D.Geometry&gt; &lt;/GeometryModel3D&gt; &lt;/ModelVisual3D.Content&gt; &lt;ModelVisual3D.Transform&gt; &lt;Transform3DGroup&gt; &lt;TranslateTransform3D OffsetX=&quot;0&quot; OffsetY=&quot;0&quot; OffsetZ=&quot;0&quot;/&gt; &lt;ScaleTransform3D ScaleX=&quot;1&quot; ScaleY=&quot;1&quot; ScaleZ=&quot;1&quot;/&gt; &lt;RotateTransform3D d:EulerAngles=&quot;0,0,0&quot;&gt; &lt;RotateTransform3D.Rotation&gt; &lt;AxisAngleRotation3D Angle=&quot;0&quot; Axis=&quot;0,1,0&quot;/&gt; &lt;/RotateTransform3D.Rotation&gt; &lt;/RotateTransform3D&gt; &lt;TranslateTransform3D OffsetX=&quot;0&quot; OffsetY=&quot;0&quot; OffsetZ=&quot;0&quot;/&gt; &lt;TranslateTransform3D OffsetX=&quot;0&quot; OffsetY=&quot;0&quot; OffsetZ=&quot;0&quot;/&gt; &lt;/Transform3DGroup&gt; &lt;/ModelVisual3D.Transform&gt; &lt;/ModelVisual3D&gt; &lt;/ModelVisual3D&gt; &lt;/ModelVisual3D&gt; &lt;/ModelVisual3D&gt; &lt;/ModelVisual3D&gt; &lt;/Viewport3D&gt; </pre> <br/></div> <div><br/></div> </div>Tue, 20 Oct 2009 06:07:02 Z2009-10-20T06:07:02Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5c226430-c54d-45b8-a8a2-7e4a79e3692a#141cddee-3509-4f62-8b67-7ce7ba46ca1bhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5c226430-c54d-45b8-a8a2-7e4a79e3692a#141cddee-3509-4f62-8b67-7ce7ba46ca1bRob Relyeahttp://social.msdn.microsoft.com/Profile/en-US/?user=Rob%20RelyeaHow to get x:Name from 3DElements using XamlReader.LoadThe Window (or other class) with a namescope, needs to be the root element that you load...not the root element of the page that has your button.<br/><br/>Perhaps you could make Viewport3DWithNameScope, make that the root of the 3d xaml file, and then all should be happy.<br/>Thanks, RobTue, 20 Oct 2009 18:06:48 Z2009-10-20T18:06:48Zhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5c226430-c54d-45b8-a8a2-7e4a79e3692a#4c0e8815-aeea-440a-a2ae-956c4f7501abhttp://social.msdn.microsoft.com/Forums/en-US/wpf/thread/5c226430-c54d-45b8-a8a2-7e4a79e3692a#4c0e8815-aeea-440a-a2ae-956c4f7501abEeeeEfffhttp://social.msdn.microsoft.com/Profile/en-US/?user=EeeeEfffHow to get x:Name from 3DElements using XamlReader.LoadGot it - sorry for the confusion, I didn't quite get that the XAML file that's <em>loaded</em> needed to have the NameScope implementer as its root.  Thanks very much.   Awesome help, as usual. <div><br/></div> <div>E.</div> <div><br/></div> <div>-------------</div> <div><br/></div> <div>some time later...  thought I'd share my final solution (thanks to Rob's suggestions):</div> <div>1. I created a Grid which implemented INameScope</div> <div> <pre lang="x-c#"> public class NameScopedGrid : Grid, INameScope { Dictionary&lt;string, object&gt; nameTable = new Dictionary&lt;string, object&gt;(); object INameScope.FindName(string name) { object element = null; nameTable.TryGetValue(name, out element); return element; } void INameScope.RegisterName(string name, object scopedElement) { nameTable[name] = scopedElement; } void INameScope.UnregisterName(string name) { nameTable[name] = null; } } </pre> <br/></div> <div>2. created 2 strings, one to prepend to my xaml stream and to append to it:</div> <div> <pre lang="x-c#"> string prepend = &quot;&lt;local:NameScopedGrid xmlns=\&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation\&quot; xmlns:x=\&quot;http://schemas.microsoft.com/winfx/2006/xaml\&quot; xmlns:d=\&quot;http://schemas.microsoft.com/expression/blend/2008\&quot; xmlns:mc=\&quot;http://schemas.openxmlformats.org/markup-compatibility/2006\&quot; xmlns:local=\&quot;clr-namespace:LoadXAML3D;assembly=LoadXAML3D\&quot;&gt;&quot;; string append = &quot;&lt;/local:NameScopedGrid&gt;&quot;; </pre> <br/></div> <div>3. created a memory stream, prepended the 1st string above, appended the contents of the xaml file (read into a byte[]), and appended the 2nd string above.</div> <div> <pre lang="x-c#"> int count = 0; byte[] byteData = null; // create a memory stream MemoryStream memStream = new MemoryStream(1024); // Write the first string to the stream. byteData = Encoding.ASCII.GetBytes(prepend); memStream.Write(byteData, 0, byteData.Length); memStream.Seek(0, SeekOrigin.End); // read the XAML file contents byte[] fileData = readFile(fileName); // Write the file contents to the stream, byte by byte. count = 0; while (count &lt; fileData.Length) { memStream.WriteByte(fileData[count++]); } memStream.Seek(0, SeekOrigin.End); // Write the second string to the stream. byteData = Encoding.ASCII.GetBytes(append); memStream.Write(byteData, 0, byteData.Length); memStream.Seek(0, SeekOrigin.Begin); </pre> </div> <div><br/></div> <div>4. read the stream, and place the resulting NameScopedGrid into the UI.</div> <div> <pre lang="x-c#"> // read the stream using XamlReader, and cast the result to NameScopedGrid. // this ends up populating NameScopedGrid.nameTable with the named elements in the XAML file NameScopedGrid grid = (NameScopedGrid)System.Windows.Markup.XamlReader.Load(memStream); // don't forget to close the memory stream memStream.Close(); // add the NameScopedGrid to the UI - in this case, to my Border object host.Child = grid; </pre> <br/></div> <div>Awesome.</div> <div><br/></div> <div><br/></div> <div><br/></div>Tue, 20 Oct 2009 19:43:25 Z2009-10-21T07:01:40Z