Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use DataTemplate.LoadContent(). Example:</p> <pre><code>DataTemplate dataTemplate = this.Resources["MyDataTemplate"] as DataTemplate; FrameworkElement frameworkElement = dataTemplate.LoadContent() as FrameworkElement; frameworkElement.DataContext = myPOCOInstance; LayoutRoot.Children.Add(frameworkElement); </code></pre> <p><a href="http://msdn.microsoft.com/en-us/library/system.windows.frameworktemplate.loadcontent.aspx">http://msdn.microsoft.com/en-us/library/system.windows.frameworktemplate.loadcontent.aspx</a></p> <p>If you have a DataTemplate defined for all instances of a type (DataType={x:Type ...}, but no x:Key="...") then you can create content using the appropriate DataTemplate using the following static method. This method also emulates ContentControl by returning a TextBlock if no DataTemplate is found.</p> <pre><code>/// &lt;summary&gt; /// Create content for an object based on a DataType scoped DataTemplate /// &lt;/summary&gt; /// &lt;param name="sourceObject"&gt;Object to create the content from&lt;/param&gt; /// &lt;param name="resourceDictionary"&gt;ResourceDictionary to search for the DataTemplate&lt;/param&gt; /// &lt;returns&gt;Returns the root element of the content&lt;/returns&gt; public static FrameworkElement CreateFrameworkElementFromObject(object sourceObject, ResourceDictionary resourceDictionary) { // Find a DataTemplate defined for the DataType DataTemplate dataTemplate = resourceDictionary[new DataTemplateKey(sourceObject.GetType())] as DataTemplate; if (dataTemplate != null) { // Load the content for the DataTemplate FrameworkElement frameworkElement = dataTemplate.LoadContent() as FrameworkElement; // Set the DataContext of the loaded content to the supplied object frameworkElement.DataContext = sourceObject; // Return the content return frameworkElement; } // Return a TextBlock if no DataTemplate is found for the source object data type TextBlock textBlock = new TextBlock(); Binding binding = new Binding(String.Empty); binding.Source = sourceObject; textBlock.SetBinding(TextBlock.TextProperty, binding); return textBlock; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload