Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
855
XamTree inside XamOutlookBar: NullReferenceException
posted

Populating a tree with code works if the tree is on root, but not if tree is in a outlookbargroup. See attached code sample.

SilverlightApplication2.zip
Parents
No Data
Reply
  • 34510
    Verified Answer
    Offline posted

    Hi Casper,

    The reason it doesn't work from within the OutlookBarGroup is because of the way names work in XAML.  More info can be found below but the gist of it is that the XamOutlookBar establishes its own namescope boundary so calling FindName from the Home page will not explore that part of the object tree.  FindName is called automatically in order to fill in the names generated by the x:Name and Name XAML attributes.  Since FindName can't reach the XamTree, the generated field that represents it in code-behind will be null.

    MSDN article on how namescopes work:
    http://msdn.microsoft.com/en-us/library/cc189026(v=vs.95).aspx#UsingNamescopesatRuntime

    Take a look at the first bullet point in the Remarks section:
    http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.findname(v=vs.95).aspx

    In order to fix your sample so the tree is populated, you could use the sender that is passed as an argument to the Loaded event and cast that to a XamTree.  Also, if you intend to use the XamTree elsewhere in code-behind you may just want to set the field manually.  Add this line of code in your loaded event before you use the XamTree:

    this.xamTree2 = sender as Infragistics.Controls.Menus.XamTree;

    Now you can use the field xamTree2 in your code-behind without any issues.  Let me know if you have any questions about this.

Children