none
WPF Frame Control RRS feed

  • Question

  • I am trying to load  xaml contained in a string variable into a Frame control like I can load an xml document via LoadXml method on the XmlDocument object or set innerXAML like I can set the innerHTML of a DIV.  I can not find any clean way to do this.  In my application the user selects an item from the left nav list and based on what is selected I have to generate xaml on the fly and bind it to a frame or any other control that will allow me to do that.  I have to take this approach because the layout of the detail screen depends on what the user selects in the left nav.  The screens can be drastically different from item to item.  In the past we have been able to accomplish this by passing the xml results thru an xslt thus converting it to html which to assigned to the innerHTML property of a div.  Any ideas, hints on how can accomplish this would be appreciated. 

    Also is there enough demand for the need to load the frames content not from a uri only , but also from a string/stream objects.


    Thanks
    mk
    Sunday, June 1, 2008 6:24 AM

Answers

  • Frame can accepts any type of content, Frame is derived from ContentControl, and it has the same content model as ContentControl. So you could directly set Frame.Content property as you do with ContentControl.Content. You can also use Frame.Navigate() method which accepts any type of object/content as parameter.

    So you could use XamlReader.Load() method to deserialize the XAML stream passed in, and this method will return an object which presents the root element you specified in the XAML stream, and you can directly assign this object to the Frame.Content property or pass it to Frame.Navigate() method.

    Hope this helps
    • Marked as answer by Marco Zhou Friday, June 6, 2008 9:48 AM
    Tuesday, June 3, 2008 2:56 AM

All replies

  • The frame control accepts only pages as contet.

    To generate and display content dynamicly you have to make sure that your XAML root element is a page.

    Benjamin
    Sunday, June 1, 2008 12:19 PM
  • The root element is a Page.  What I am complaining about is the fact that the file has to be loaded from disk. Frame control does not give me an option to set the Source or Content property of the Frame  from a string.  Does any one know how to set the Frames content from xaml contained in a string or a stream?

    Regards

    mk
    Monday, June 2, 2008 4:28 AM
  • Frame can accepts any type of content, Frame is derived from ContentControl, and it has the same content model as ContentControl. So you could directly set Frame.Content property as you do with ContentControl.Content. You can also use Frame.Navigate() method which accepts any type of object/content as parameter.

    So you could use XamlReader.Load() method to deserialize the XAML stream passed in, and this method will return an object which presents the root element you specified in the XAML stream, and you can directly assign this object to the Frame.Content property or pass it to Frame.Navigate() method.

    Hope this helps
    • Marked as answer by Marco Zhou Friday, June 6, 2008 9:48 AM
    Tuesday, June 3, 2008 2:56 AM
  • Thanks Marco that definitely helps.  I had already found a work around the issue by using XamlReader.Load to load the xaml into a grids content.  I tried assigning a string to the content property of the frame that did not work, but assigning the content property the result of a XamlReader.Load worked just fine.

    Is there any way other than creating a derived Frame control to communicate with the page's datacontect contained inside the Frame, or can the page see the Frame's datacontext?

    Regards

    mk
    Wednesday, June 4, 2008 2:10 AM
  • ->Is there any way other than creating a derived Frame control to communicate with the page's datacontect contained inside the Frame, or can the page see the Frame's datacontext?

    Well, this is another interesting aspect of Frame control, I think you'd better start a new thread on this question:)

    Thanks
    Marco
    Wednesday, June 4, 2008 9:20 AM