Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6353895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:29:22+00:00 2026-05-24T22:29:22+00:00

When you assign an object to a Content control it will materialize a Visual

  • 0

When you assign an object to a Content control it will materialize a Visual appropriate for that assigned object. Is there a programmatic way to achieve the same result? I would like to call a function in WPF with an object and get back a Visual, where the same logic is applied in generating the Visual as if you had supplied the object to a Content control instance.

For example, if I have a POCO object and assign it to a Content control and there happens to be an appropriate DataTemplate defined then it materializes that template to create the Visual. I would like my code to be able to take a POCO object and get back from WPF the Visual.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T22:29:24+00:00Added an answer on May 24, 2026 at 10:29 pm

    Use DataTemplate.LoadContent(). Example:

    DataTemplate dataTemplate = this.Resources["MyDataTemplate"] as DataTemplate;
    FrameworkElement frameworkElement = dataTemplate.LoadContent() as FrameworkElement;
    frameworkElement.DataContext = myPOCOInstance;
    
    LayoutRoot.Children.Add(frameworkElement);
    

    http://msdn.microsoft.com/en-us/library/system.windows.frameworktemplate.loadcontent.aspx

    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.

    /// <summary>
    /// Create content for an object based on a DataType scoped DataTemplate
    /// </summary>
    /// <param name="sourceObject">Object to create the content from</param>
    /// <param name="resourceDictionary">ResourceDictionary to search for the DataTemplate</param>
    /// <returns>Returns the root element of the content</returns>
    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple object that allows you to assign three properties (x,y,z) (lets
In a WPF app, is there a object I can assign to FileSystemWatcher.SynchronizingObject? I
Hello (Sorry for my bad English) Is it bad practice to assign object to
In Perl, it's often nice to be able to assign an object, but specify
Suppose you create a generic Object variable and assign it to a specific instance.
I want to assign a class attribute via a string object - but how?
How do i dynamically assign a name to a php object? for example how
Is it bad javascript practice to not assign a newly created object to a
I have an object with a string[] property. When I assign this as the
Seem to be having an issue with std::auto_ptr and assignment, such that the object

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.